Skip to content

Instantly share code, notes, and snippets.

@n1lesh
Last active October 19, 2019 14:32
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save n1lesh/199c10ba965b7ca42e09eb4c43da498a to your computer and use it in GitHub Desktop.
Save n1lesh/199c10ba965b7ca42e09eb4c43da498a 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);
}
});
@wizard1066
Copy link

Also did this ..

npm i express -g

  • express@4.16.2

@dukedrake
Copy link

"npm i express -g" installs to "global" scope and hence require('express') doesn't work
You need to install express like this:
npm i express

@jrpool
Copy link

jrpool commented Dec 7, 2017

My implementation of this procedure has failed, though a parallel implementation with Nginx succeeded. With Node/Express I get a handshake failure that I have not yet managed to diagnose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment