Skip to content

Instantly share code, notes, and snippets.

@chingchai
Created October 13, 2020 13:38
Show Gist options
  • Save chingchai/3d4fbb5fbc07589d2ef61de46f8983e9 to your computer and use it in GitHub Desktop.
Save chingchai/3d4fbb5fbc07589d2ef61de46f8983e9 to your computer and use it in GitHub Desktop.
HTTPS-NodeServer
// Dependencies
const fs = require('fs');
const http = require('http');
const https = require('https');
const express = require('express');
const app = express();
// Certificate
const privateKey = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/privkey.pem', 'utf8');
const certificate = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/cert.pem', 'utf8');
const ca = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/chain.pem', 'utf8');
const credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
app.use((req, res) => {
res.send('Hello there !');
});
// Starting both http & https servers
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
httpServer.listen(80, () => {
console.log('HTTP Server running on port 80');
});
httpsServer.listen(443, () => {
console.log('HTTPS Server running on port 443');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment