localhost https express

Cover image

Try this

openssl req -nodes -new -x509 -keyout server.key -out server.cert
const express = require('express')
const fs = require('fs')
const https = require('https')
const app = express()

app.get('/',  (req, res) => res.send('hello world')


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

or

openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem
npx http-server -S -C cert.pem

Bonus:

You may get a TLS error when using another npm or node process to communicate with your shiny new self signed server. On development try setting this environment variable

NODE_TLS_REJECT_UNAUTHORIZED=0 node index.js
1 people liked this postDavid Pich

1 people reposted this postDavid Pich