gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 23:49:34 25 4
gpt4 key购买 nike

我几乎是在学习本教程(我发现的所有其他教程看起来都一样)

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com