javascript Node.js socket.io.js 未找到或 io 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10563080/
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
Node.js socket.io.js not found or io not defined
提问by Hyman TC
I'm trying to run a node.js application on my freebsd server, but I can't get the socket.io library to work with it. I've tried including:
我正在尝试在我的 freebsd 服务器上运行 node.js 应用程序,但我无法使用 socket.io 库来使用它。我试过包括:
<script src="/socket.io/socket.io.js"></script>
Which gives a 404 error, and if i link directly to the file (i.e. where it is in my public_html folder) I get the io not defined error.
这给出了 404 错误,如果我直接链接到文件(即它在我的 public_html 文件夹中的位置),我会收到 io 未定义错误。
Thanks in advance
提前致谢
回答by sonikarc
Try creating another node.js application that has this single line in it and then run it with node.js
尝试创建另一个包含这一行的 node.js 应用程序,然后使用 node.js 运行它
var io = require('socket.io').listen(8000);
Then in your browser visit http://127.0.0.1:8000
and you should get the friendly "Welcome to socket.io." greeting. If you are getting this then socket.io is running and will serve the socket.io.js file.
然后在您的浏览器中访问http://127.0.0.1:8000
,您应该会看到友好的“欢迎使用 socket.io”。问候。如果你得到这个,那么 socket.io 正在运行,并将提供 socket.io.js 文件。
The only other thing that I can think of that might be happening is that you might not be linking to the alternate port in your client file. Unless you're running the socket.io server on express which is running on port 80. For now create a client file that has the script source for socket.io set to
我能想到的唯一其他可能发生的事情是您可能没有链接到客户端文件中的备用端口。除非你在 express 上运行 socket.io 服务器,它在端口 80 上运行。现在创建一个客户端文件,将 socket.io 的脚本源设置为
<script src="http://127.0.0.1:8000/socket.io/socket.io.js"> </script>
This should connect to the socket.io server running on port 8000 and get the socket.io.js file.
这应该连接到在端口 8000 上运行的 socket.io 服务器并获取 socket.io.js 文件。
回答by Amadan
Your node.js application still has to serve it - it does not get served automagically. What do you have in your server? It should be something like
您的 node.js 应用程序仍然必须为其提供服务 - 它不会自动获得服务。你的服务器里有什么?它应该是这样的
var app = require('express').createServer();
var io = require('socket.io').listen(app);
or similar (the listen
is important). The location is not a real location on the disk - socket.io library should intercept the URL and serve its clientside library, as far as I understand.
或类似(这listen
很重要)。该位置不是磁盘上的真实位置 - 据我所知,socket.io 库应该拦截 URL 并为其客户端库提供服务。
回答by vimdude
Add the following after body parser:
在正文解析器之后添加以下内容:
, express.static(__dirname + "/public")
So something like:
所以像:
var app = module.exports = express.createServer(
express.bodyParser()
, express.static(__dirname + "/public")
);