javascript - node.js: socket.io vs express.static - Stack Overflow

I have the following server.js running:module.exports = server;var express = require('express'

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' (menting out express.static and express.logger lines) socket.io.js is served correctly. How can I bine both functionalities?

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' (menting out express.static and express.logger lines) socket.io.js is served correctly. How can I bine both functionalities?

Share Improve this question asked Apr 19, 2012 at 15:39 daaanipmdaaanipm 2733 silver badges8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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);

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

[email protected] 
[email protected] 
[email protected]

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744895867a4599692.html

相关推荐

  • javascript - node.js: socket.io vs express.static - Stack Overflow

    I have the following server.js running:module.exports = server;var express = require('express'

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信