Last active
February 28, 2018 11:25
-
-
Save LM1LC3N7/6478c8b85ea8f3b535d711aba576f8d0 to your computer and use it in GitHub Desktop.
Start a new expressjs https web server, using a valid certificate or a new auto generated self signed one and handle SIGINT to exit gracefully
This file contains hidden or 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
// | |
// To test this: | |
// 1) npm install express fs https pem | |
// 2) node https_srv.js | |
// 3) open a new webpage on http://127.0.0.1 | |
// | |
// Provide here a valid key file and crt file to run a valid https server | |
// Invalid path will force the app to generate a self signed certificate | |
let certsFiles = { | |
key: './127.0.0.1.key', | |
crt: './127.0.0.1.cert' | |
}; | |
// Ports numbers | |
const HTTP_PORT = 80; | |
const HTTPS_PORT = 443; | |
// Dependancies | |
let fs = require('fs'); | |
let http = require('http'); | |
let https = require('https'); | |
let pem = require('pem'); | |
let express = require('express'); | |
let app = express(); | |
let httpServer = http.createServer(app); | |
let httpsServer = null; | |
let privateKey = null; | |
let certificate = null; | |
// Error handling | |
process.on('uncaughtException', function (err) { | |
if (err && (typeof err.errno !== 'undefined') && (err.errno === 'EADDRINUSE') && (isNaN(err.port) === false)) { | |
console.error('Cannot start server on port ' + err.port + ', port is already in use.'); | |
} else { | |
console.error(err); | |
console.log('\n\n'); | |
console.log('Error detected, exiting the app...'); | |
process.exit(1); | |
} | |
}); | |
// | |
// your express configuration here | |
// | |
// | |
// Test if a valid certificate has been provided | |
// or generate a self signed one | |
// | |
if (fs.existsSync(certsFiles.key)) { | |
privateKey = fs.readFileSync(certsFiles.key, 'utf8'); | |
} | |
if (fs.existsSync(certsFiles.crt)) { | |
certificate = fs.readFileSync(certsFiles.crt, 'utf8'); | |
} | |
// Valid certificate: start an https server | |
if ((privateKey !== null) && (certificate !== null)) { | |
console.log('Valid certificate provided, starting the https server.'); | |
httpsServer = https.createServer({ key: privateKey, cert: certificate }, app).listen(HTTPS_PORT, function (err) { | |
if (err) throw err; | |
else console.log('Server HTTPS started on port ' + HTTPS_PORT); | |
}); | |
} else { | |
// Generate a self signed certificate | |
// Will be SHA 256 + RSA 2048 | |
console.log('No valid certificate provided, creating a self-signed certificate...'); | |
pem.createCertificate({ days: 365, selfSigned: true }, function (err, keys) { | |
if (err) { | |
console.error('Cannot create a self-signed certificate.'); | |
throw err; | |
} | |
httpsServer = https.createServer({ key: keys.serviceKey, cert: keys.certificate }, app).listen(HTTPS_PORT, function (err) { | |
if (err) throw err; | |
else console.log('Server HTTPS started on port ' + HTTPS_PORT); | |
}); | |
}); | |
} | |
// Redirect all HTTP to HTTPS | |
httpServer.listen(HTTP_PORT, function (err) { | |
if (err) throw err; | |
else console.log('Server HTTP (used to redirect to HTTPS) started on port ' + HTTP_PORT); | |
}); | |
app.all('*', function (req, res, next) { | |
if (req.secure) { | |
return next(); | |
} | |
if (HTTPS_PORT !== 443) res.redirect('https://' + req.hostname + ':' + HTTPS_PORT + req.url); | |
else res.redirect('https://' + req.hostname + req.url); | |
}); | |
// Handle ^C | |
process.on('SIGINT', shutdown); | |
process.on('SIGHUP', shutdown); | |
process.on('SIGQUIT', shutdown); | |
process.on('SIGABRT', shutdown); | |
process.on('SIGTERM', shutdown); | |
// Do graceful shutdown | |
function shutdown () { | |
httpServer.close(function () { | |
console.log('Http server closed.'); | |
}); | |
httpsServer.close(function () { | |
console.log('Https server closed.'); | |
}); | |
} | |
// | |
// App | |
// | |
app.get('/', function (req, res) { | |
res.send('This is an https page!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment