Skip to content

Instantly share code, notes, and snippets.

Last active Mar 17, 2022
What would you like to do?
Use https certificate on localhost with Express

Use https certificate on localhost with Express

Note: tested on Ubuntu

Install utilities

sudo apt install libnss3-tools
chmod +x mkcert-v1.4.3-linux-amd64 
sudo mv mkcert-v1.4.3-linux-amd64 /usr/local/bin/mkcert

Create certificate for your app

# If you do not want to install certifictaes to your browsers
# mkcert -install localhost
# If you want to make trusted certificates to your browsers
# If you have a firefox master password, you should enter it
# when you see "Enter Password or Pin for "NSS Certificate DB""
mkcert -install localhost

Install an run the app

npm i
node app.js

Then, go to https://localhost:3001/

const express = require('express');
const https = require('https');
const fs = require('fs');
const key = fs.readFileSync('./localhost-key.pem');
const cert = fs.readFileSync('./localhost.pem');
const app = express();
const server = https.createServer({key: key, cert: cert }, app);
app.get('/', (req, res) => {
res.send('this is an secure server');
server.listen(3001, () => {
console.log('listening on 3001');
"name": "my-secure-app",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment