Skip to content

Instantly share code, notes, and snippets.

@artginzburg
Created October 3, 2021 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artginzburg/9d39e345a2c1d44f074b3e7f7fec7576 to your computer and use it in GitHub Desktop.
Save artginzburg/9d39e345a2c1d44f074b3e7f7fec7576 to your computer and use it in GitHub Desktop.
HTTPS with Express.js
openssl req -nodes -new -x509 -keyout server.key -out server.cert
const https = require('https');
const fs = require('fs');
const app = require('express')();
const { PORT = 3001, HOST = 'localhost' } = process.env;
app.get('/', (req, res) => {
res.send('Hello, world!');
})
// - We'd normally do:
// app.listen(PORT, () => console.log(`API listening on http://${HOST}:${PORT}`));
// - But instead:
https.createServer({
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.cert'),
}, app)
.listen(PORT, () => console.log(`API listening on https://${HOST}:${PORT}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment