Nodejs https与cpanel结合

时间:2018-08-01 09:13:00

标签: node.js ssl cpanel

我试图像这样在我的nodejs服务器上使https工作:

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网址有效,https超时了。 知道为什么会这样吗?

我曾经使用过cpanel,所以我已经在那儿设置了ssl证书,但是当涉及到节点时,我遇到了麻烦。

2 个答案:

答案 0 :(得分:0)

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);

希望有帮助。

答案 1 :(得分:0)

问题在于端口未转发。