我最近尝试在我的服务器上安装SSL证书。证书文件(privkey.pem、fullchain.pem)位于应用程序的根目录中。当我运行以下代码时:
var express = require('express');
var app = express();
var helmet = require('helmet');
var db = require('./server/database.js');
var fs = require('fs');
var ssl = require('ssl-root-cas');
'use strict';
var rootCas = require('ssl-root-cas/latest').create();
// default for all https requests
// (whether using https directly, request, or another module)
require('https').globalAgent.options.ca = rootCas;
app.use(helmet());
var options = {
key : fs.readFileSync('privkey.pem', 'ascii'),
cert : fs.readFileSync('fullchain.pem', 'ascii')
}
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
app.use('/public', express.static(__dirname + '/public'));
var serv = require('https').createServer(options, app);
服务器运行时没有错误。我添加的"Server is listening port 80“确认显示,该证书似乎不会导致任何直接问题。然而,当我尝试连接到域时(使用https://) ),Chrome会响应ERR_CONNECTION_REFUSED。当通过http连接到域名时,Chrome会回复相同的消息。我使用的是SocketIO,它在后面的代码中初始化,我没有发现我的问题和SocketIO的函数之间有任何联系。导致无法连接的原因是什么?
发布于 2017-09-22 19:52:39
https请求通过端口443而不是80发送。以下代码运行正常:
var express = require('express');
var app = express();
var helmet = require('helmet');
var db = require('./server/database.js');
var fs = require('fs');
var ssl = require('ssl-root-cas');
'use strict';
var rootCas = require('ssl-root-cas/latest').create();
// default for all https requests
// (whether using https directly, request, or another module)
require('https').globalAgent.options.ca = rootCas;
app.use(helmet());
var options = {
key : fs.readFileSync('privkey.pem', 'ascii'),
cert : fs.readFileSync('fullchain.pem', 'ascii')
}
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
app.use('/public', express.static(__dirname + '/public'));
var serv = require('https').createServer(options, app);
//var serv = require('https').Server(app); //DEBUG ONLY
https://stackoverflow.com/questions/46356715
复制相似问题