D
D
Dmitry2021-05-13 14:22:53
Node.js
Dmitry, 2021-05-13 14:22:53

How to properly set up an https server on nodejs?

I put up a server like this:

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

const PathToKey = './ssl/localhost-100y.key';
const PathToCert = './ssl/localhost-100y.cert';

const httpsOptions = {
    key: fs.readFileSync(PathToKey),
    cert: fs.readFileSync(PathToCert)
};

const app = function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}

http.createServer(app).listen(8080);
https.createServer(httpsOptions, app).listen(443);


If you switch to 8080, then everything is OK, but if you switch to 443, it writes ERR_EMPTY_RESPONSE. I don’t understand what’s the matter, I’ve already shoveled a bunch of examples.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question