Skip to content

Instantly share code, notes, and snippets.

@jackfiallos
Forked from n1lesh/NodeHTTPs1.js
Created November 19, 2018 03:40
Show Gist options
  • Save jackfiallos/ea3fb8520af8db5ae41b9d20aad4342a to your computer and use it in GitHub Desktop.
Save jackfiallos/ea3fb8520af8db5ae41b9d20aad4342a to your computer and use it in GitHub Desktop.
HTTPs Server with Node.js and Express
var express = require('express');
var app = express();
var fs = require('fs');
var key = fs.readFileSync('encryption/private.key');
var cert = fs.readFileSync( 'encryption/primary.crt' );
var ca = fs.readFileSync( 'encryption/intermediate.crt' );
var options = {
key: key,
cert: cert,
ca: ca
};
var https = require('https');
https.createServer(options, app).listen(443);
var http = require('http');
http.createServer(app).listen(80);
app.use(function(req, res, next) {
if (req.secure) {
next();
} else {
res.redirect('https://' + req.headers.host + req.url);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment