Setup HTTPS for local development using node.js and express.

🕑February 02, 2021

In this article we are going to test our site locally in https using mkcert in node & express app. mkcert is a great tool to automatically generate and install SSL certificates.

Make sure we have node.js installed in our machine. You can download it here

🔧 Installation & Steps

  1. Install Homebrew (package manager for mac os)
# only for mac users
/bin/bash -c "$(curl -fsSL"
  1. Install mkcert to generate all TLS certificates.

For mac

brew install mkcert
brew install nss # if you use Firefox

For windows : Follow this instructions to install the mkcert.

  1. Generate trusted cerificate locally
$ mkcert -install
# Created a new local CA
$ mkcert [domain name]
# In our case, we will use localhost
$ mkcert localhost

Above step will generate couple of files [domainname]-key.pem, [domainname].pem.

  1. Create an node app and create server js file.
npm init
touch server.js
  1. Install express js
npm install express
  1. Copy these files created in step 3 [domainname]-key.pem, [domainname].pem and paste it in this folder.
  2. Include this below code in server.js
"use strict"
const https = require("https")
const express = require("express")
const fs = require("fs")
// load the self-signed certificates
const options = {
  key: fs.readFileSync("[domainname]-key.pem"),
  cert: fs.readFileSync("[domainname].pem"),
const app = express()
app.get("/", (req, res, next) => {
  res.send("Localhost hosted in https !")
https.createServer(options, app).listen(3000)
  1. Start the server
node server.js
  1. Test the application by opening the browser and hitting the URL

💽 Test output in the browser

Output Test localhost in https

📚 Reference

  1. mkcert official documentation
  2. How to use local https