8

I have the following server.js running:

module.exports = server;

var express = require('express');
var fs = require('fs');

var server = express.createServer();    

var port = 58000;
server.listen(port);

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

server.use(express.static('/', __dirname + '/../public'));

server.use(express.logger());

io.on('connection', function(client){
    console.log('new client connected ' + client);
    client.on('message', function(){
        console.log('client wants something');
    });
});

Simple express.static server for files in a /public subfolder, plus socket.io functionality. With this setup, any request for the 'socket.io.js' file fails, i.e.

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

returns a 404 error (file not found). Static file server works correctly. If I simply use the 'http' module instead of 'express' (commenting out express.static and express.logger lines) socket.io.js is served correctly. How can I combine both functionalities?

daaanipm
  • 273
  • 3
  • 8

2 Answers2

3

Express 3.0.0 (lastest) change its API.

Here is a question very similar to yours that delivers the response.

var express = require('express')
  , http = require('http');

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

...

server.listen(8000);
Community
  • 1
  • 1
Arnaud Rinquin
  • 4,296
  • 1
  • 30
  • 51
0

Make sure you have the last versions of express.js and of socket.io.js. My side it's working great with

express@2.5.8 
socket.io@0.8.5 
node@0.6.5

Otherwise, a solution can be to call var io = require('socket.io').listen(server); after your server.use

Tronix117
  • 1,985
  • 14
  • 19
  • express@3.0.0alpha1 may not have been a good idea... working now – daaanipm Apr 19 '12 at 16:01
  • Oh, in fact, there is some big troubles with express@3 for the moment, mainly on the router and middlewares, they are working on it. You should limit yourself to stable version (current is @2.5.9), if you don't plan to fork express.js – Tronix117 Apr 19 '12 at 16:08