W
W
WhatIsHTML2018-06-01 07:57:29
Node.js
WhatIsHTML, 2018-06-01 07:57:29

NodeJS Express SSL connection error?

Hello. I'm trying to run NodeJS with an SSL certificate. Code snippet

const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');

const app = express();

const http = require('http');
const https = require('https');
const fs = require('fs');

// require our custom dependencies
const router = require('./router');
const PORT = process.env.PORT || 8000;

app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/../public'));
app.use(router);

//NOT WORK
if (process.env.PROD) {
  const options = {
    key: fs.readFileSync('path/to/private-key.key'),
    cert: fs.readFileSync('path/to/cert.crt'),
    // optional
    ca: [
      fs.readFileSync('path/to/COMODORSADomainValidationSecureServerCA.crt'),
      fs.readFileSync('path/to/COMODORSAAddTrustCA.crt')
    ]
  };

  https.createServer(options, app).listen(PORT, function () {
    console.log('PROD app listening on port', PORT);
  });

} 
// WORK
else {
  http.createServer(app).listen(PORT, function () {
    console.log('Example app listening on port', PORT);
  });
}

NodeJS starts up, but the site itself does not work at all - it gives ERR_EMPTY_RESPONSE if I access it via http
and ERR_CONNECTION_REFUSED if I access it via https. If you replace the launch of NodeJS with a regular http - the site works.
Sites that check for key and certificate compliance like https://sslanalyzer.comodoca.com/ do not work, because site lies

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Wolf, 2018-06-01
@WhatIsHTML

PORT 443 is specified?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question