Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

socket.io + express 3.0 #1096

Closed
bscarvell opened this Issue Apr 20, 2012 · 7 comments

Comments

Projects
None yet
6 participants

Hey there TJ, socket.io keeps throwing 404 errors only when running on version 3.0 (server is fine, its just the route on the client)

Previous versions work fine with it. Any suggestions how I can get it work for the client side?

For anyone else having this issue, this hack can be applied to get around it:

var fs = require('fs');
app.get('/socket.io/socket.io.js', function(req, res) {
    fs.readFile('/PROJECT_HOME/node_modules/socket.io/lib/socket.io.js', function(error, content) {
        if (error) {
            res.writeHead(500);
            res.end();
        }
        else {
            res.writeHead(200, { 'Content-Type': 'text/javascript' });
            res.end(content, 'utf-8');
        }
   });
});

Thanks @deltanovember

Owner

tj commented Apr 20, 2012

http.createServer(app), an express app is a Function now that's probably why

@tj tj closed this Apr 20, 2012

var app = express();
var server = app.listen(3000);
var io = require('socket.io').listen(server);

stanxii commented Apr 23, 2012

Hi, dbainbridge, Thanks a lot! it works fine!

So the following code would be acceptable when wanting to fire a callback on startup?

var server = app.listen(settings.port, function(){ 
    console.log("Express server listening on port %d in %s mode", settings.port, env);
});

var io = require('socket.io').listen(server);

This is what I am using which is a little clearer:

var app = express();
var server = app.listen();
var io = require('socket.io').listen(server);
.
.
.
server.listen( function() {
console.log("Express server listening on port %d in %s mode", server.address().port, app.settings.env);
});

I'm not explicitly specifying a port because I am using https://github.com/learnboost/up which will specify the port for me.

I think that the examples for express should be using a callback on listen to indicate the server is listening only when it actually is.

The other snippet that should be included in examples is the following:

server.addListener('error', function(err) {
console.log(err);
});

I haven't fleshed this out yet to catch the common errors and propagate if it isn't handled here but one of the things this will allow you to do is catch if the port address is invalid or already in use.. and not dump a stack trace! I've seen a lot of people misinterpret that common "error" with a stack trace as a bug..

On May 3, 2012, at 10:33 AM, Scotty Vernon wrote:

So the following code would be acceptable when wanting to fire a callback on startup?

var server = app.listen(settings.port, function(){ 
   console.log("Express server listening on port %d in %s mode", settings.port, env);
});

var io = require('socket.io').listen(server);

Reply to this email directly or view it on GitHub:
visionmedia#1096 (comment)

Hi, dbainbridge. Thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment