简体   繁体   中英

How to setup SSL under node express socket.io?

I basically have node.js code that start off like this:

var app = require('express')();
var http = require('http').createServer(app).listen(5051, function() {
    console.log('listening on 127.0.0.1:5051');
});
var io = require('socket.io')(http);
var lobby = io.of('/lobby');

lobby.on("connection", function (socket) {
    console.log("Successful connection lobby");
});

So I start my server, I start my app, get a connection, no problem. Trying to add SSL however is not working. I followed the certificate creating instructions from this website using the internal address as the common name.

var app = require('express')();
var fs = require('fs');
var options = {
    key     : fs.readFileSync('/"path_to"/hacksparrow-key.pem'),
    cert    : fs.readFileSync('/"path_to"/hacksparrow-cert.pem') 
};
var https = require('https').createServer(options,app).listen(5051, function() {
    console.log('listening on 127.0.0.1:5051');
});
var io = require('socket.io')(https);
var lobby = io.of('/lobby');
lobby.on("connection", function (socket) {
        console.log("Successful connection lobby");
    });

Made sure my client used https instead of http, no connection, nothing. Anyone have any idea why this is happening, also, if I were using something like Nginx and used ssl on it, would this step even be necessary?

It sounds like the problem is that nginx is sending an http requests to your node https server.

If nginx was set up to use ssl then it would not be necessary for node to use ssl. Both servers could use ssl if you want them to.

Your certificate will generally be generated with a passphrase. This is required to initiate the SSL.

So when you start the app, node.js will prompt for it once. You can bypass that by adding "passphrase" key in your variable "options"

var options = {
    key     : fs.readFileSync('/"path_to"/hacksparrow-key.pem'),
    cert    : fs.readFileSync('/"path_to"/hacksparrow-cert.pem'),
    passphrase: type_your_certificate's_passphrase
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM