ZL
About Articles Contact
Published on Feb 21, 2022
Filed under:
#node,
#express

Serving HTTPS locally with Node

You won’t need to serve up HTTPS when developing locally because localhost is treated like a secure context.

But you need to use a HTTPS scheme even on localhost in some cases — like when you’re trying to work with Facebook’s API.

We’re going to talk about how to serve up a HTTPS website on localhost. It’s quite simple. Really.

First you need to install mkcert. You can do this with Homebrew if you’re on a Mac.

Terminal window
brew install mkcert
brew install nss # Required for Firefox

Then you need to run the mkcert install command, which creates a certificate authority on your computer.

mkcert -install

Next, you can run mkcert with localhost or any other ip address you want. This generates the certificates. If you’re using Node, it’s best to run this command within your project.

Terminal window
# Generates certificates for localhost and 127.0.0.1
mkcert localhost 127.0.0.1

This creates two pem files.

I like to put these files in a certs folder and rename them into key.pem and cert.pem to keep things tidy.

The final step is to serve them in your server.

If you use Express, you can use the following code:

import express from 'express'
import https from 'https'
import path from 'path'
import fs from 'fs'
import { fileURLToPath } from 'url'
const app = express()
const __dirname = path.dirname(fileURLToPath(import.meta.url))
// Express routes here
// ...
// Listen with SSL
const server = https.createServer(
{
key: fs.readFileSync(`${__dirname}/certs/key.pem`, 'utf8'),
cert: fs.readFileSync(`${__dirname}/certs/cert.pem`, 'utf8'),
},
app,
)
server.listen(443, _ => {
console.log('App listening at https://localhost')
})

Final thing to do is load your site at https://localhost and you’re done!

One final thing: You can also use port 80 instead of 443. But if you use port 80, you need to load the site at https://localhost:80.

Here’s a demo for you to play with :)

That’s it!

Previous Best way to install Node and keep it up to date Next Never let anyone stop you from pursuing your dreams

Join My Newsletter

I share what I’m learning on this newsletter: code, building businesses, and living well.

Sometimes I write about technical deep-dives, product updates, musings on how to live, and sometimes my struggles and how I’m breaking through.

Regardless of the type of content, I do my best to send you an update every week.

If you’re into making things and growing as a person, you’ll probably feel at home here.

“

Zell is one of those rare people who commands tremendous knowledge and experience but remains humble and helpful. They want you to know what they know, not just be impressed by it.

In other words, Zell is a natural teacher. You’re lucky to have him because he feels lucky to be able to help you in your journey.

Heydon Pickering
Heydon Pickering — Web & Accessibility Extraordinaire
The Footer

General

Home About Contact Testimonials Tools I Use

Projects

Magical Dev School Splendid Labz

Socials

Youtube Instagram Tiktok Github Bluesky X

Follow Along

Email RSS
© 2013 - 2025 Zell Liew / All rights reserved / Terms