Skip to main content
100%

Use https certificate on localhost with Express

✓ Published0🌍 Public
TThomasG77
Last edited Mar 17, 2022
Created on Mar 17, 2022

This example shows how to run an Express web server over HTTPS on localhost, delivering a plain-text response. It creates a Node.js application that reads SSL certificate files (`localhost-key.pem` and `localhost.pem`) generated by the mkcert utility, then uses the `https.createServer` method with those credentials. The Express app defines a single route for the root path, and the server listens on port 3001. The code demonstrates the required setup steps, including installing mkcert and generating a trusted local certificate.

AI-generated description

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/

ISC Licensed

Similar vizzes