无法在express.js中设置https

时间:2012-07-19 18:37:16

标签: node.js ssl https express

我非常关注本教程(我发现的所有其他教程看起来都一样)

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

我的代码如下:

// 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'));
});

应用程序在sudo节点应用程序

之后启动
Express server listening on port 443

现在我卷曲

curl https://localhost/

我得到了

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

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

由于现在通过npm发布的Express 3.x,“app()” - 应用程序功能已更改。有https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x的迁移信息。 Express 2.x SSL教程都不再适用。快递3.x的正确代码是:

// 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'));
});