To start an HTTPS server, you need to have the private key.pem and also the certificate.pem of the SSL certificate.

Then using the crypto and fs libraries, you can get the contents of the files and start a server that have SSL enabled.

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.setSecure(credentials);
server.addListener("request", handler);
server.listen(443);

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.