Skip to content

Instantly share code, notes, and snippets.

@artginzburg
Created October 3, 2021 15:26
Embed
What would you like to do?
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