Node.js: socket.io vs express.static

The following server.js works for me:

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 / subfolder, plus socket.io function. With this setting, any request for socket.io.js is not executed, i.e.

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

returns 404 error (file not found). The static file server is working correctly. If I just use the "http" module instead of the "express" (commenting out express.static and express.logger lines), socket.io.js is served correctly. How can I combine both functions?

+8
javascript express
source share
2 answers

Express 3.0.0 (the latter) changes its API.

Here is a question very similar to yours that delivers an answer.

 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); 
+3
source share

Make sure you have the latest versions of express.js and socket.io.js. My side works great with

 express@2.5.8 socket.io@0.8.5 node@0.6.5 

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

0
source share

All Articles