Last active
December 15, 2016 04:46
-
-
Save magician11/f66a84b5e24a91f14c5fa57aab4fe8ee to your computer and use it in GitHub Desktop.
How to add https to your Node.js server
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
// libraries | |
const express = require('express'); | |
const https = require('https'); | |
const fs = require('fs'); | |
const cors = require('cors'); // Cross-Origin Resource Sharing | |
// setup express | |
const app = express(); | |
app.use(cors()); | |
// your routes | |
app.get('/some-route', (req, res) => { | |
// your logic | |
const yourData = { text: 'my data' }; | |
res.json(yourData); | |
}); | |
app.set('port', 3003); | |
// Add in the path to the Let's Encrypt files | |
const sslOptions = { | |
key: fs.readFileSync('/etc/letsencrypt/live/golightlyplus.com/privkey.pem'), | |
cert: fs.readFileSync('/etc/letsencrypt/live/golightlyplus.com/fullchain.pem'), | |
ca: fs.readFileSync('/etc/letsencrypt/live/golightlyplus.com/chain.pem'), | |
}; | |
// startup the https server | |
https.createServer(sslOptions, app).listen(app.get('port'), () => { | |
// eslint-disable-next-line no-console | |
console.log(`Started app on port ${app.get('port')}.`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment