找不到 node.js/socket.io/socket.io.js [英] node.js /socket.io/socket.io.js not found
问题描述
我不断收到错误消息/socket.io/socket.io.js 404(未找到)未捕获的 ReferenceError:io 未定义
i keep on getting the error /socket.io/socket.io.js 404 (Not Found) Uncaught ReferenceError: io is not defined
我的代码是
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);
和
<script src="/socket.io/socket.io.js"></script>
什么问题???
欢迎任何帮助!
推荐答案
Copying socket.io.js
到公共文件夹(类似于 resources/js/socket.io.js
) 不是正确的方法.
Copying socket.io.js
to a public folder (something as resources/js/socket.io.js
) is not the proper way to do it.
如果 Socket.io
服务器正确侦听您的 HTTP
服务器,它将自动通过 http://localhost:<port> 提供客户端文件;/socket.io/socket.io.js
,您不需要在可公开访问的文件夹中找到它或将其复制为 resources/js/socket.io.js
&手动提供.
If Socket.io
server listens properly to your HTTP
server, 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.
代码示例
Express 3.x -Express 3 要求您实例化一个 http.Server
以将 socket.io
附加到第一个
Code sample
Express 3.x -
Express 3 requires that you instantiate a http.Server
to attach socket.io
to first
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);
快乐编码:)
这篇关于找不到 node.js/socket.io/socket.io.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!