Javascript 找不到 socket.io.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10191048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:14:35  来源:igfitidea点击:

socket.io.js not found

javascriptnode.jsexpresssocket.io

提问by dan-lee

For some reason my node server cannot serve the route /socket.io/socket.io.js, I always get a 404 error.
I tried compiling different node versions (current is 0.6.13 which also runs on server, where it actually works).
From the app.js I get info: socket.io startedand no error when trying to call the socket.io.js.

由于某种原因,我的节点服务器无法为路由提供服务/socket.io/socket.io.js,我总是收到 404 错误。
我尝试编译不同的节点版本(当前是 0.6.13,它也在服务器上运行,它实际工作的地方)。
从 app.js 中我得到info: socket.io started并且在尝试调用 socket.io.js 时没有错误。

I try it from localhost and port 8000 and I use the express framework

我从本地主机和端口 8000 尝试它,我使用 express 框架

This is the code from app.js:

这是来自 app.js 的代码:

var express = require('express')
  , app = require('express').createServer()
  , io = require('socket.io').listen(app, { log: true });

app.listen(8000);

app.configure(function() {
    app.use(express.static(__dirname + '/public'));
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

io.sockets.on('connection', function (socket) {
   // all other stuff here

回答by nguyenkha

Please check your Express version. Express recently is updated to 3.0alpha which API was changed. If 3.0 you can change your code to something likes this:

请检查您的 Express 版本。Express 最近更新到 3.0alpha,其中 API 已更改。如果 3.0,您可以将代码更改为如下所示:

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);

Same issue with connect: https://github.com/senchalabs/connect/issues/500#issuecomment-4620773

与连接相同的问题:https: //github.com/senchalabs/connect/issues/500#issuecomment-4620773

回答by Aviram Netanel

Using with the Express 3 web framework: (from socket.io)

与 Express 3 Web 框架一起使用:(来自 socket.io

> Express 3 requires that you instantiate a http.Serverto attach socket.io to first:

> Express 3 要求您先实例化 ahttp.Server以将 socket.io 附加到:

meaning - (1) you must create a server instance:

含义 - (1) 您必须创建一个服务器实例:

var app = express();
var http = require('http').createServer(app);

(2) couple it with the socket.io:

(2) 将其与 socket.io 耦合:

var io = require('socket.io');
io.listen(http);

and ONLY THEN - (3) make the server listen:

并且只有那么 - (3) 让服务器监听:

http.listen(8080);

make sure you keep this order!

确保你保持这个顺序!

回答by r03

After installing node 0.8.1 I had the same problem. I just deleted the node_modules map in my project folder and reinstalled express/socket.io. After that it worked fine again with the code in your question.

安装节点 0.8.1 后,我遇到了同样的问题。我刚刚删除了项目文件夹中的 node_modules 映射并重新安装了 express/socket.io。之后,您的问题中的代码再次正常工作。

回答by Jacek Wysocki

Maybe this could help you, on my Ubuntu 11.10 I haven't properly set NODE_PATHvariable, If you are on linux/mac try add line below to your .bashrc/.zshrc file.

也许这可以帮助你,在我的 Ubuntu 11.10 上我没有正确设置NODE_PATH变量,如果你在 linux/mac 上尝试将下面的行添加到你的 .bashrc/.zshrc 文件中。

export NODE_PATH=/usr/lib/node_modules:$NODE_PATH

回答by Sayuri Mizuguchi

Install Socket.io inside your repository:

在您的存储库中安装 Socket.io:

npm install socket.io --save 

After, config the server:

之后,配置服务器:

   var express = require('express')
   var app     = express();
   var server  = require('http').createServer(app);
   var io      = require('socket.io').listen(server);

   server.listen(app.get('80')); // not 'app.listen'

And inside your archive HTML/EJSor another you want, add:

在您的档案HTML/EJS或其他您想要的档案中,添加:

<script src="/socket.io/socket.io.js"></script>

Check if works with Console(Chrome/ Mozilla, etc).

检查是否适用于Console(Chrome/Mozilla 等)。

In my example I use Chrome (Ctrl + shift + I):

在我的示例中,我使用 Chrome (Ctrl + shift + I):

enter image description here

在此处输入图片说明