node.js - 无法在 express.js 中设置 https

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

Express server listening on port 443

现在当我 curl

curl https://localhost/

我明白了

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

有什么想法吗?

最佳答案

自从现在通过 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'));
});

关于node.js - 无法在 express.js 中设置 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11567217/

相关文章:

node.js - 如何使用 nodejs 进行 dojo 构建?

android - 无法将Android上的socket io与服务器nodejs连接

php - 我可以从 HTTP 客户端获取 TLS secret 来解密我自己的 HTTPS 对话吗?

Azure API 管理服务不会将 header 中的客户端证书传递到后端

ssl - 为 Rails 2 中的所有页面设置 ssl_allowed/ssl_required?

node.js - PM2 生产优化

javascript - gulp-live-server 不刷新页面

c++ - 有错误的 Cpp OpenSSL 服务器的源代码

Python LDAP : The signature algorithm is not supported

php - 忽略 Zend_Http_Client 中的 SSL 错误