Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active March 17, 2022 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/acf9ffd02220f0555f9e034c7c220aa9 to your computer and use it in GitHub Desktop.
Save ThomasG77/acf9ffd02220f0555f9e034c7c220aa9 to your computer and use it in GitHub Desktop.
Use https certificate on localhost with Express
node_modules/

Use https certificate on localhost with Express

Note: tested on Ubuntu

Install utilities

sudo apt install libnss3-tools
wget https://github.com/FiloSottile/mkcert/releases/download/v1.4.3/mkcert-v1.4.3-linux-amd64
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