Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@magician11
Last active December 15, 2016 04:46
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 magician11/f66a84b5e24a91f14c5fa57aab4fe8ee to your computer and use it in GitHub Desktop.
Save magician11/f66a84b5e24a91f14c5fa57aab4fe8ee to your computer and use it in GitHub Desktop.
How to add https to your Node.js server
// 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