找不到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>
有什么问题???
欢迎任何帮助!
推荐答案
复制 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
,您无需在可公开访问的文件夹中找到它或复制as 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屋!