murvinlai murvinlai - 2 months ago 16x
Node.js Question

How to create an HTTPS server in Node.js?

Given an SSL key and certificate, how does one create an HTTPS service?


I found following example. It is a few months old, but it might be good:

Directly from that source:

const crypto = require('crypto'),
  fs = require("fs"),
  http = require("http");

var privateKey = fs.readFileSync('privatekey.pem').toString();
var certificate = fs.readFileSync('certificate.pem').toString();

var credentials = crypto.createCredentials({key: privateKey, cert: certificate});

var handler = function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');

var server = http.createServer();
server.addListener("request", handler);