Created
October 3, 2021 15:26
-
-
Save artginzburg/9d39e345a2c1d44f074b3e7f7fec7576 to your computer and use it in GitHub Desktop.
HTTPS with Express.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
openssl req -nodes -new -x509 -keyout server.key -out server.cert |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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