相关疑难解决方法(0)

如何使用Express/Socket.io在Node.js上使用HTTPS

我试图用https运行我的节点服务器.我正在使用express和socket.io.

这是我的https代码:

var httpsPort = 443;
var privateKey = fs.readFileSync(mykeypath');
var certificate = fs.readFileSync(mycertificatepath');
var credentials = {key: privateKey, cert: certificate};
var https = require('https').Server(credentials,app);
var io = require('socket.io')(https);

https.listen(httpsPort, function(){
logger.info('listening on *:' + httpsPort);
});


app.get('/initGame', function (req,res){

var slots = require('./slots.json', 'utf8');
var userObject = {
    address : req.connection.remoteAddress,
    userAgent : req.headers['user-agent']
};
db.getPlayedGames(userObject,function(playedGames){
    logger.debug(playedGames);
    if(typeof playedGames == 'undefined' ){
        playedGames=0;
    }else{
        playedGames = playedGames.games_played;
    }
    var spinsLeft = 10-playedGames;
    res.json({
        spinsLeft: spinsLeft,
        slots: slots
    });
  }); …
Run Code Online (Sandbox Code Playgroud)

https node.js express socket.io

26
推荐指数
1
解决办法
5万
查看次数

'缺少PFX或证书+私钥.' 在https socket.io中

我已经有这个问题已经有一段时间了但是无法弄明白......我没有尝试过任何真正有用的东西.从ssl文件转换为许多不同的格式并检查文件夹权限没有任何效果(或者,更明确的是,它应该是工作的一切).有谁知道什么可能会关闭?非常感谢你的帮助,我现在正处于绝望的一面......

这可能与版本有关吗?我怎么能检查是否是这种情况?

这是我的代码:

var ssl_options = {
    pfx : fs.readFileSync(my_pfx_path),
    passphrase: 'password'
};

//OR

var ssl_options = {
    key : fs.readFileSync(my_key_path),
    cert : fs.readFileSync(my_cert_path)
};

var protocol = "https";

preparedApp = require(protocol).createServer(ssl_options,app);

var io = require('socket.io')(preparedApp);

preparedApp.listen(8080, function(){});
io.on('connection', function(socket){});
Run Code Online (Sandbox Code Playgroud)

这是我的ssl_options的日志...

{ key: <Buffer 41 ...>,
 cert: <Buffer 4a ...> }
Run Code Online (Sandbox Code Playgroud)

这与标题中的错误有关throw new Error('Missing PFX or certificate + private key.');.

完整跟踪日志:

 Error: Missing PFX or certificate + private key.
at Server (tls.js:1127:11)
at new Server (https.js:35:14)
at …
Run Code Online (Sandbox Code Playgroud)

ssl socket.io

6
推荐指数
1
解决办法
287
查看次数

标签 统计

socket.io ×2

express ×1

https ×1

node.js ×1

ssl ×1