Skip to content

Instantly share code, notes, and snippets.

@purplejacket
Created August 4, 2016 23:44
Show Gist options
  • Select an option

  • Save purplejacket/7a756f5cde330381d9db50698ed7ef52 to your computer and use it in GitHub Desktop.

Select an option

Save purplejacket/7a756f5cde330381d9db50698ed7ef52 to your computer and use it in GitHub Desktop.
A small node program to test https using a pfx file. Note: uses port 8443 so as not to disrupt the production instance.
var url = require('url');
var path = require('path');
var express = require('express');
var http = require('http');
var https = require('https');
var fs = require('fs');
var moment = require('moment');
require('log-timestamp'); // this will cause console.log to prepend timestamps
var app = express();
var port = process.env.TLS == true ? 8443 : 8080;
function startListening() {
function secureServer() {
var pfx_file = process.env['PFX_CERT_FILE'];
if (!pfx_file) {
console.log("Need environment var PFX_CERT_FILE");
process.exit(1);
}
var pfx = fs.readFileSync(pfx_file);
return https.createServer({pfx}, app);
}
var server;
var port;
if (process.env['TLS'] !== 'true') {
port = process.env['NODE_ENV'] == 'production' ? 80 : 8080;
server = http.createServer(app);
} else {
port = 8443;
server = secureServer();
}
server.listen(port);
console.log('Listening on port', port);
// do something like server.on('error', onError) ?
}
app.get('/', function(req, res) {
var now = "Date: " + moment().format("YYYY-MM-DD HH:mm:ss");
console.log("received request for route / at " + now);
res.send("serving route / at " + now );
});
startListening();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment