3

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

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 node app 之后启动正常

Express server listening on port 443

现在当我卷曲

curl https://localhost/

我明白了

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

有任何想法吗?

4

1 回答 1

3

从现在通过 npm 发布的 Express 3.x 开始,“app()”-应用程序函数发生了变化。https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x上有迁移信息。express 2.x SSL 教程不再适用。express 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'));
});
于 2012-07-19T19:17:55.613 回答