Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

wizard1066 commented Oct 17, 2017

Followed these instructions and got this ...

host:web localuser$ ls
apps.js mydomain.csr private.key
host:web localuser$ node apps.js
module.js:529
throw err;
^
Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:527:15)
at Function.Module._load (module.js:476:23)
at Module.require (module.js:568:17)
at require (internal/module.js:11:18)
at Object. (/Users/localuser/web/apps.js:1:77)
at Module._compile (module.js:624:30)
at Object.Module._extensions..js (module.js:635:10)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
at Function.Module._load (module.js:500:3)

Which isn't really ideal, running ...

host:web localuser$ uname -r
16.7.0
host:web localuser$ node -v
v8.7.0
host:web localuser$ npm -v
5.4.2

@wizard1066

This comment has been minimized.

Copy link

wizard1066 commented Oct 17, 2017

Also did this ..

npm i express -g

  • express@4.16.2
@dukedrake

This comment has been minimized.

Copy link

dukedrake commented Dec 5, 2017

"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

This comment has been minimized.

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