gpt4 book ai didi

javascript - ssl 版本或密码不匹配 ExpressJS

转载 作者:行者123 更新时间:2023-12-04 19:27:35 24 4
gpt4 key购买 nike

我在 express 中遇到 https 的问题,我不太明白:

这是我的上一个项目(它有效):

index.js:

var fs = require('fs');

var http = require('http');
var https = require('https');

var privateKey = fs.readFileSync(__dirname + '/cert/server.key', 'utf8');
var certificate = fs.readFileSync(__dirname + '/cert/server.cert', 'utf8');

var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

httpServer.listen(80);
httpsServer.listen(443);

//Force https
app.use(function(req, res, next) {
if(!req.secure) {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
next();
});

这是来自我当前的项目(不起作用):

index.js:
var fs = require('fs');

var configRaw = fs.readFileSync(__dirname + '/config.json', 'utf8', function(err, data) {
if(err) {
return abort('Can not read/ find' + __dirname + '/config.json'); //FIXME
}
});

var config = JSON.parse(configRaw);

console.log(' - loading webserver script..');
var webServer = require(__dirname + '/lib/web_server.js');

webServer.init(config.httpPort, config.httpsPort);

web_server.js:
var fs = require('fs');
var http = require('http');
var https = require('https');

var privateKey = fs.readFileSync(__dirname + '/../ssl/server.key', 'utf8');
var certificate = fs.readFileSync(__dirname + '/../ssl/server.cert', 'utf8');

var credential = { key: privateKey, vert: certificate };
var express = require('express');
var app = express();

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credential, app);

this.init = function(httpPort, httpsPort){
httpServer.listen(httpPort);
httpsServer.listen(httpsPort);
}

app.use(function(req, res, next) {
if(!req.secure) {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
next();
});

我使用与以前相同的证书和 key ,并且在读取文件或其他内容时从未出现 fs 错误。

但是当我通过浏览器访问 localhost 时,我会收到以下消息:ERR_SSL_VERSION_OR_CIPHER_MISMATCH。

请记住,我对 Node.js 还很陌生 - 非常感谢您! ;)

最佳答案

看起来你有一个错字:

var credential = { key: privateKey, vert: certificate };

“vert”而不是“cert” :)

关于javascript - ssl 版本或密码不匹配 ExpressJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69618575/

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