node.js:socket.io vs express.static

时间:2022-08-22 16:52:36

I have the following server.js running:

我有以下server.js运行:

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.

用于/ public子文件夹中文件的简单express.static服务器,以及socket.io功能。使用此设置,对'socket.io.js'文件的任何请求都会失败,即

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?

返回404错误(找不到文件)。静态文件服务器正常工作。如果我只使用'http'模块而不是'express'(注释掉express.static和express.logger行),则正确提供了socket.io.js。我如何结合两种功能?

2 个解决方案

#1


3  

Express 3.0.0 (lastest) change its API.

Express 3.0.0(最新)更改其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);

#2


0  

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

确保你有最新版本的express.js和socket.io.js。我的方面很有效

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

否则,解决方案可以是调用var io = require('socket.io')。listen(server);在你的server.use之后

#1


3  

Express 3.0.0 (lastest) change its API.

Express 3.0.0(最新)更改其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);

#2


0  

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

确保你有最新版本的express.js和socket.io.js。我的方面很有效

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

否则,解决方案可以是调用var io = require('socket.io')。listen(server);在你的server.use之后