0

这是我用来连接http服务器的代码。

var app = require('http').createServer(require('express')),
io = require('socket.io').listen(app),
util = require('util'),
connectionsArray = [], // maintain active connected client details
connectionStatistics = {'summary': {'instance_count': 0, 'user_count': 0, 'customer_count': 0}, 'customers': {}}, // for debugging purpose

server_port = 3000, // port on which nodejs engine to run
POLLING_INTERVAL = 10 * 1000, // 10 sec
pollingTimer = [], // timeouts for connected sockets
fs = require('fs'), // lib for file related operations
log_file = {
    'error': fs.createWriteStream(__dirname + '/debug.log', {flags: 'a'}), // file to log error messages
    'info': fs.createWriteStream(__dirname + '/info.log', {flags: 'a'}) // file to log info messages
};




var server = app.listen(server_port, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log('Please use your browser to navigate to http://%s:%s', host, port);
});

我想在上面的代码中包含 https 连接。我尝试使用 SSLCertificateFile 和 SSLCertificateKeyFile 连接 https。

但这对我不起作用。

4

2 回答 2

0

使用 express 和 https 模块而不是 http 尝试此代码段

let fs = require('fs');

let https = require('https');

let express = require('express');

let app = express();

let options = { key: fs.readFileSync('./file.pem'), cert: fs.readFileSync('./file.crt') };

let serverPort = 3000;

let server = https.createServer(options, app);

let io = require('socket.io')(server);

io.on('connection', function(socket) { console.log('new connection'); });

server.listen(serverPort, function() { console.log('server up and running at %s port', serverPort); });

于 2020-01-03T09:57:55.137 回答
0
  1. 安装 https 模块 ( yarn add https / npm i https )
  2. 更改选项(ssl 文件路径)如下:
    常量选项 = {

            密钥:fs.readFileSync('./ssl/private.key'),
            证书:fs.readFileSync('./ssl/certificate.crt'),
            ca:fs.readFileSync('./ssl/ca_bundle.crt')
    }
    https.createServer(options, app).listen(port);
于 2020-01-03T09:40:41.733 回答