3

I'm pretty much following this tutorial (all other tutorials I found look the same)

http://www.hacksparrow.com/express-js-https.html

My code is as follows:

// dependencies
var express = require('express')
  , https = require('https')
  , fs = require('fs');

var privateKey = fs.readFileSync('./ssl/rp-key.pem').toString();
var certificate = fs.readFileSync('./ssl/rp-cert.pem').toString();

var app = express.createServer({
  key : privateKey
, cert : certificate
});

...

// start server
https.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

The app starts fine after sudo node app

Express server listening on port 443

Now when I curl

curl https://localhost/

I get

curl: (35) Unknown SSL protocol error in connection to localhost:443

Any ideas?

Max
  • 1,399
  • 13
  • 28

1 Answers1

3

Since Express 3.x, which is now being published via npm, the "app()"-Application Function changed. There is an migration info on https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x . None of the express 2.x SSL tutorials will work anymore. The correct Code for express 3.x is:

// dependencies
var express = require('express')
  , https = require('https')
  , fs = require('fs');

var privateKey = fs.readFileSync('./ssl/rp-key.pem').toString();
var certificate = fs.readFileSync('./ssl/rp-cert.pem').toString();

var options = {
  key : privateKey
, cert : certificate
}
var app = express();

...

// start server
https.createServer(options,app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});
Chris
  • 4,204
  • 1
  • 25
  • 30
  • https.createServer part was generated by express; it was http.createServer(app).listen ... by default; lemme try that though – Max Jul 19 '12 at 19:23
  • 1
    It must be your key/certs then. How do you create them? – Chris Jul 19 '12 at 19:26
  • i just followed the tutorial exactly – Max Jul 19 '12 at 19:31
  • when generating the certrequest, i left a few questions empty, would that cause an issue? – Max Jul 19 '12 at 19:32
  • 1
    I edited my post, check https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x (Application Function) for more info. I think this will be a popular question in the future ;) – Chris Jul 19 '12 at 19:55