Express HTTPS server with a self-signed certificate
npm install
npm start
Check this 😄 TUTORIAL
Run this command in your project directory to generate certificate and key. If using Ubuntu you will have openssl installed by default. If on MacOS you can install it with: brew install openssl
openssl req -nodes -new -x509 -keyout server.key -out server.cert
The utilitiy will walk you through the process. Just remember to put localhost on the common name section.
Then we can create a simple node server with express.
const https = require("https");
const express = require("express");
const app = express();
const fs = require("fs");
const port = 3000;
app.get("/", (req, res) => {
res.send("Hello HTTPS!");
});
https.createServer(
{
key: fs.readFileSync("server.key"),
cert: fs.readFileSync("server.cert")
},
app
)
.listen(port, () => {
console.log(`Example app listening on port ${port}!`);
});
Click on Advanced and proceed to page. You can see that localhost is using the https protocol, although it's a self-signed certificate hence it's not legit but you get the gist.