I'm trying to adapt the node server included with the angular-seed project to serve SSL. Based on this example where they have:
// HTTPS
var https = require('https');
// read in the private key and certificate
var pk = fs.readFileSync('./privatekey.pem');
var pc = fs.readFileSync('./certificate.pem');
var opts = { key: pk, cert: pc };
// create the secure server
var serv = https.createServer(opts, function(req, res) {
console.log(req);
res.end();
});
// listen on port 443
serv.listen(443, '0.0.0.0');
I tried the following, which appears to run (no errors are logged), however when I navigate to https://localhost:8000/home I get "This webpage is not available" http://localhost:8000/home- non SSL - worked before I hacked the node server. How can I get this to work as SSL?
#!/usr/bin/env node
var util = require('util'),
http = require('http'),
fs = require('fs'),
url = require('url'),
https = require('https'),
events = require('events');
// read in the private key and certificate
var pk = fs.readFileSync('./scripts/privatekey.pem');
var pc = fs.readFileSync('./scripts/certificate.pem');
var opts = { key: pk, cert: pc };
var DEFAULT_PORT = 8000;
function main(argv) {
// create the secure server
new HttpServer({ key: pk, cert: pc,
'GET': createServlet(StaticServlet),
'HEAD': createServlet(StaticServlet)
}).start(Number(argv[2]) || DEFAULT_PORT);
}
[... balance of script omitted ...]
The node server code defines
function HttpServer(handlers) {
this.handlers = handlers;
this.server = http.createServer(this.handleRequest_.bind(this));
}
which it then goes on to extend as for example:
HttpServer.prototype.start = function(port) {
this.port = port;
this.server.listen(port);
util.puts('Http Server running at http://localhost:' + port + '/');
};
Based on a further reading of the node docs it's this fragement: http.createServer(this.handleRequest_.bind(this)) that would have to become https.createServer(this.handleRequest_.bind(this)) -- I'm still not sure where / how to pass the certificate keys into the process.
Given that an equivalent express ssl server would be easier to figure out, I think I'll just switch. (The following is untested. I'll update as needed once I've tested.)
var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
// This line is from the Node.js HTTPS documentation.
var options = {
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
// Create a service (the app object is just a callback).
var app = express();
app.use(express.static(__dirname));
// Create an HTTP service.
// http.createServer(app).listen(80);
// Create an HTTPS service identical to the HTTP service.
https.createServer(options, app).listen(8000);