murvinlai murvinlai - 1 year ago 168
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?

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download