gpt4 book ai didi

node.js - Nodejs https 结合 cpanel

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:17 26 4
gpt4 key购买 nike

我正在尝试让 https 像这样在我的 nodejs 服务器上工作:

var http = require('http');
var https = require('https');
var fs = require('fs');
var express = require('express');
var privateKey = fs.readFileSync('server.key', 'utf8');
var certificate = fs.readFileSync('server.crt', 'utf8');

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

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

httpServer.listen(8080, ()=> {
console.log('Server started and listening on port 8080...')
});
httpsServer.listen(8443, ()=>{
console.log('Server started and listening on port 8443...')
});

当我运行服务器时只有 http url 在工作,https 会超时。知道为什么会这样吗?

我习惯使用 cpanel,所以我已经在那里设置了 ssl 证书,但是当涉及到 node 时,我就碰壁了。

最佳答案

您的 ssl 文件似乎有问题,您需要在创建 https 服务器时提供证书文件和私钥,如下所示:

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

// This line is from the Node.js HTTPS documentation.
var options = {
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.cert')
};

// Create a service (the app object is just a callback).
var app = express();

// Create an HTTP service.
http.createServer(app).listen(80);
// Create an HTTPS service identical to the HTTP service.
https.createServer(options, app).listen(443);

希望对您有所帮助。

关于node.js - Nodejs https 结合 cpanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51629493/

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