Adding HTTPS to NodeJS + SocketIO

Viewed 151

I am creating an app using NodeJS and SocketIO and wanted to add HTTPS support. Currently, my app works as intended with http.

var http = require('http');
var socketIO = require('socket.io');

var fileServer = new(nodeStatic.Server)();    
var server = http.createServer(function(req, res) {
    fileServer.serve(req, res);
}).listen(8080);

var io = socketIO.listen(server);

io.sockets.on('connection', function(socket) {
...

This has worked perfectly for me. I tried adding HTTPS support with the following:

var https = require('https');
var fs = require('fs');
var socketIO = require('socket.io');

var options = {
    key: fs.readFileSync('key.pem'),
    cert: fs.readFileSync('cert.pem')
};

var fileServer = new(nodeStatic.Server)();
var server = https.createServer(options, function(req, res) {
    fileServer.serve(req, res);
}).listen(8080);

var io = socketIO.listen(server);

io.sockets.on('connection', function(socket) {
...

The following code throws the error Failed to load resource: net::ERR_EMPTY_RESPONSE and Uncaught ReferenceError: io is not defined.

Any advice would really be appreciated, I'm still learning NodeJS and SocketIO. Thanks!

0 Answers
Related