Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js: socket.io vs express.static

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?

like image 647
daaanipm Avatar asked Apr 19 '12 15:04

daaanipm


1 Answers

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);
like image 166
Arnaud Rinquin Avatar answered Nov 14 '22 22:11

Arnaud Rinquin