Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS Express not serving '/socket.io/socket.io.js'

I am trying to build NodeJS/Express/SocketIO application.

Imports:

var     express     = require('express')
    ,   app         = express()
    ,   server      = require('http').createServer(app)
    ,   io          = require('socket.io').listen(server)...

Configuration:

app.configure(function(){
    app.set('port', process.env.PORT || 3000);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.cookieParser('your secret here'));
    app.use(express.session());
    app.use(app.router);
    app.use(express.static(path.join(__dirname, 'public')));
});

In my Jade template:

script(type='text/javascript', href='/socket.io/socket.io.js')

But '/socket.io/socket.io.js' is not available when I am trying to reach it on

http://localhost:3000/socket.io/socket.io.js

As it says:

Cannot GET /socket.io/socket.io.js

How do I serve socket.io.js from the installed module?

Thanks

like image 997
Max Golovanchuk Avatar asked Nov 19 '12 16:11

Max Golovanchuk


3 Answers

You need to copy the client code from an inner Socket.io directory to your public directory. In Socket 0.9, the path to the distributable files is node_modules/socket.io/node_modules/socket.io-client/dist.

like image 114
Nelson Avatar answered Nov 10 '22 20:11

Nelson


You do not have to copy any files, socket.io will take care of the delivery of the file.

var express = require('express'),
    app     = new express(),
    server  = require('http').createServer(app),
    io      = require('socket.io').listen(server)
like image 34
Julian Lannigan Avatar answered Nov 10 '22 19:11

Julian Lannigan


In Express 3/4 you can initialize socket.io like so:

var io = require("socket.io")(app.listen(80));
like image 42
nick Avatar answered Nov 10 '22 19:11

nick