🕑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
# only for mac users
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
For mac
brew install mkcert
brew install nss # if you use Firefox
For windows : Follow this instructions to install the mkcert.
$ 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
.
npm init
touch server.js
npm install express
[domainname]-key.pem
, [domainname].pem
and paste it in this folder."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)
node server.js
https://localhost:3000/