我一直在检查其他相关答案,例如:
- > node.js /socket.io/socket.io.js not found
- > Socket.io not being served by Node.js server
- > socket.io.js not found on client
- > Configuring 'simplest' node.js + socket.IO + Express server
但我没有找到解决问题的方法。
这正是我所做的:
通过 windows 可执行文件安装 Node ,然后:
express node6 --hogan --ejs
cd node6
npm install
npm install socket.io
npm start
我的 app.js 是默认的,但我添加了以下行以将其与 socket.io 链接:
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io')(server);
io.listen(http);
http.listen(3000);
Here's the complete app.js file .
我还尝试复制并粘贴Node http server 和Using with Express 3/4 的示例或详细的 app.js in the socket.io docs , 但它们都在调用 npm start
时抛出错误。
然后,在我的 hoggn View 中,我尝试以这种方式加载 socket.io:
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
据我所知,node.js 应该会自动获取它,但在我的情况下不会。为什么? Express 版本:4.2.0
这是错误页面:
Not Found
404
Error: Not Found
at Layer.app.use.res.render.message [as handle] (C:\inetpub\wwwroot\node6\app.js:29:15)
at trim_prefix (C:\inetpub\wwwroot\node6\node_modules\express\lib\router\index.js:240:15)
at C:\inetpub\wwwroot\node6\node_modules\express\lib\router\index.js:208:9
at Function.proto.process_params (C:\inetpub\wwwroot\node6\node_modules\express\lib\router\index.js:269:12)
at next (C:\inetpub\wwwroot\node6\node_modules\express\lib\router\index.js:199:19)
at next (C:\inetpub\wwwroot\node6\node_modules\express\lib\router\index.js:176:38)
at C:\inetpub\wwwroot\node6\node_modules\express\lib\router\index.js:137:5
at C:\inetpub\wwwroot\node6\node_modules\express\lib\router\index.js:250:10
at next (C:\inetpub\wwwroot\node6\node_modules\express\lib\router\index.js:160:14)
at next (C:\inetpub\wwwroot\node6\node_modules\express\lib\router\index.js:176:38)
最佳答案
不是执行 npm install socket.io
你必须执行 npm install socket.io --save
这样 socket.io 模块就会安装到你的 web 开发文件夹中(在基本位置/您的 index.html 或 index.php 所在的位置运行此命令)。这会将 socket.io 安装到命令运行的区域,而不是全局,此外,它会自动更正/更新您的 package.json 文件,以便 node.js 知道它在那里。
然后将源路径从 '/socket.io/socket.io.js'
更改为 'http://' + location.hostname + ':3000/socket.io/socket.io.js'
.
关于javascript - 找不到 Socket.io.js(node.js + express + socket.io),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25525177/