找不到 node.js /socket.io/socket.io.js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19426882/
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/socket.io.js not found
提问by hausinho
i keep on getting the error /socket.io/socket.io.js 404 (Not Found) Uncaught ReferenceError: io is not defined
我不断收到错误 /socket.io/socket.io.js 404 (Not Found) Uncaught ReferenceError: io is not defined
my code is
我的代码是
var express = require('express'), http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(3000);
and
和
<script src="/socket.io/socket.io.js"></script>
what is the problem ???
问题是什么 ???
any help is welcome!
欢迎任何帮助!
回答by Amol M Kulkarni
Copying socket.io.jsto a public folder (something as resources/js/socket.io.js) is not the proper way to do it.
复制socket.io.js到公用文件夹(如resources/js/socket.io.js)不是正确的方法。
If Socket.ioserver listens properly to your HTTPserver, it will automatically serve the client file to via http://localhost:<port>/socket.io/socket.io.js, you don't need to find it or copy in a publicly accessible folder as resources/js/socket.io.js& serve it manually.
如果Socket.io服务器正确侦听您的HTTP服务器,它将自动将客户端文件提供给 via http://localhost:<port>/socket.io/socket.io.js,您无需resources/js/socket.io.js手动查找或复制到可公开访问的文件夹中作为& 提供。
Code sample
Express 3.x-
Express 3 requires that you instantiate a http.Serverto attach socket.ioto first
代码示例
Express 3.x- Express 3 要求您先实例化 ahttp.Server以附加socket.io到
var express = require('express')
, http = require('http');
//make sure you keep this order
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
//...
server.listen(8000);
Happy Coding :)
快乐编码:)
回答by ZiTAL
How to find socket.io.js for client side
如何为客户端找到socket.io.js
install socket.io
安装socket.io
npm install socket.io
find socket.io client
找到 socket.io 客户端
find ./ | grep client | grep socket.io.js
result:
结果:
./node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js
copy socket.io.js to your resources:
将 socket.io.js 复制到您的资源中:
cp ./node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js /home/proyects/example/resources/js/
in your html:
在你的 html 中:
<script type="text/javascript" src="resources/js/socket.io.js"></script>
回答by Blubberguy22
It seems that this question may have never been answered (although it may be too late for the OP, I'll answer it for anyone who comes across it in the future and needs to solve the problem).
看来这个问题可能从来没有人回答过(虽然对OP来说可能为时已晚,但我会为以后遇到它并需要解决问题的任何人回答)。
Instead of doing npm install socket.ioyou have to do npm install socket.io --saveso the socket.io module gets installed in your web development folder (run this command at the base location/where your index.html or index.php is). This installs socket.io to the area in which the command is run, not globally, and, in addition, it automatically corrects/updates your package.json file so node.js knows that it is there.
npm install socket.io您不必npm install socket.io --save这样做,而是将 socket.io 模块安装在您的 Web 开发文件夹中(在基本位置/您的 index.html 或 index.php 所在的位置运行此命令)。这会将 socket.io 安装到运行命令的区域,而不是全局,此外,它会自动更正/更新您的 package.json 文件,以便 node.js 知道它在那里。
Then change your source path from '/socket.io/socket.io.js'to 'http://' + location.hostname + ':3000/socket.io/socket.io.js'.
然后将源路径从 更改'/socket.io/socket.io.js'为'http://' + location.hostname + ':3000/socket.io/socket.io.js'。
回答by Jordan Georgiadis
... "You might be wondering where the /socket.io/socket.io.js file comes from, since we neither add it and nor does it exist on the filesystem. This is part of the magic done by io.listen on the server. It creates a handler on the server to serve the socket.io.js script file."
...“您可能想知道 /socket.io/socket.io.js 文件从何而来,因为我们既没有添加它,也不存在于文件系统中。这是 io.listen on 完成的魔法的一部分服务器。它在服务器上创建一个处理程序来为 socket.io.js 脚本文件提供服务。”
from the book Socket.IO Real-time Web Application Development, page 56
来自 Socket.IO 实时 Web 应用程序开发一书,第 56 页
回答by Tomas Kukis
You must just follow https://socket.io/get-started/chat/and all will work.
您只需按照https://socket.io/get-started/chat/ 操作即可。
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
http.listen(3000, function(){
console.log('listening on *:3000');
});
回答by AlexanderZhao
If you are following the socket.io tutorial https://socket.io/get-started/chat/, you should add this line as below.
如果您正在关注 socket.io 教程https://socket.io/get-started/chat/,您应该添加如下这一行。
app.use(express.static(path.join(__dirname, '/')))
This is because in the tutorial, Expresswill only catch the url
/and send the file of index.html.
这是因为在教程中,Express只会抓取 url
/并发送index.html.
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html')
})
However, in the index.html, you have a script tag (<script src="/socket.io/socket.io.js"></script>) requests the resouce of socket.io-client, which is not routed in index.js(it can be found in console-network that the url is http://localhost:3000/socket.io/socket.io.js).
但是,在 中index.html,您有一个脚本标记 ( <script src="/socket.io/socket.io.js"></script>) 请求 的资源socket.io-client,该资源未路由到index.js(可以在控制台网络中找到 url 是http://localhost:3000/socket.io/socket.io.js)。
回答by jcomeau_ictx
while this doesn't have anything to do with the OP, if you're running across this issue while maintaining someone else's code, you might find that the problem is caused by the coder setting io.set('resource', '/api/socket.io');in the application script, in which case your HTML code would be <script>type="text/javascript" src="/api/socket.io/socket.io.js"></script>.
虽然这与 OP 没有任何关系,但如果您在维护其他人的代码时遇到此问题,您可能会发现问题是由io.set('resource', '/api/socket.io');应用程序脚本中的编码器设置引起的,在这种情况下,您的 HTML 代码会<script>type="text/javascript" src="/api/socket.io/socket.io.js"></script>。

