How to create a SSL certificate for localhost development in NodeJS/Express/TypeScript

Generate the required SSH keys. In the process we’ll be asked for a few questions about the organisation etc.

openssl req -x509 -newkey rsa:2048 -keyout keytmp.pem -out cert.pem -days 365
openssl rsa -in keytmp.pem -out key.pem

Now we have our certificates ready for use.

In our index.ts:

import * as https from "https";
import * as fs from "fs";
import * as express from "express";

// expressJS options
const httpsOptions = {
  key: fs.readFileSync("/Users/mimo/.openssl/key.pem"),
  cert: fs.readFileSync("/Users/mimo/.openssl//cert.pem"),
  requestCert: false,
  rejectUnauthorized: false
};

const server = https.createServer(httpsOptions, app).listen(app.get("port"), function() {
  console.log("server started at port 3000");
});