Running express.js server over HTTPS

Oct 27, 2017 · Updated: Jan 04, 2021 · by Tim Kamanin

HTTPS is everywhere and more often than not we need to spin an https server or two. Here's how you can do it for your local express.js dev server:

1. Generate a self-signed certificate

openssl req -nodes -new -x509 -keyout server.key -out server.cert

2. Enable HTTPS in Express

Add something like this to your index.js

var express = require('express')
var fs = require('fs')
var https = require('https')
var app = express()

app.get('/', function (req, res) {
  res.send('hello world')

  key: fs.readFileSync('server.key'),
  cert: fs.readFileSync('server.cert')
}, app)
.listen(3000, function () {
  console.log('Example app listening on port 3000! Go to https://localhost:3000/')

Now run a command node index.js and your server should be available at address https://localhost:3000.

Hey, if you've found this useful, please share the post to help other folks find it:

There's even more:

Subscribe for updates

  • via Twitter: @timonweb
  • old school RSS:
  • or evergreen email ↓ ↓ ↓