node.js:socket.io 与 express.static [英] node.js: socket.io vs express.static
问题描述
我正在运行以下 server.js:
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');
});
});
用于/public 子文件夹中文件的简单 express.static 服务器,以及 socket.io 功能.使用此设置,对socket.io.js"文件的任何请求都会失败,即
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
返回 404 错误(未找到文件).静态文件服务器工作正常.如果我只是使用http"模块而不是express"(注释掉 express.static 和 express.logger 行),则 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?
推荐答案
Express 3.0.0 (lastest) 更改其 API.
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);
这篇关于node.js:socket.io 与 express.static的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!