我最近尝试将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);
服务器运行时没有错误。 “服务器正在侦听端口80”确认我添加了节目,证书似乎没有引起任何直接问题。但是,当我尝试连接到域时(使用https://),Chrome会使用ERR_CONNECTION_REFUSED进行响应。通过http连接到域时,Chrome会使用相同的消息进行响应。我正在使用SocketIO,它稍后在代码中初始化,我没有在我的问题和SocketIO的函数之间找到任何连接。是什么导致无法连接?
答案 0 :(得分:0)
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