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 How AI will shape the coding ecosystem in the future Next Best practices for container queries

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 I live, and sometimes my struggles and how I’m breaking through.

Regardless of the type of content, I do my best to send you at least one insightful piece every week.

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

“

Zell is the rare sort of developer who both knows his stuff and can explain even the most technical jargon in approachable — and even fun — ways!

I’ve taken his courses and always look forward to his writing because I know I’ll walk away with something that makes me a better front-ender.

Geoff Graham
Geoff Graham — Chief Editor @ CSS Tricks
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 - 2026 Zell Liew / All rights reserved / Terms