Socket.io.js not found (node.js + express + socket.io)

Socket.io.js not found (node.js + express + socket.io)

本文关键字:js io express socket not found Socket node      更新时间:2023-09-26

我一直在检查其他相关的答案,例如:

  • 找不到node.js/ssocket.io/socket.io.js
  • Node.js服务器未为Socket.io提供服务
  • 在客户端上找不到socket.io.js
  • 正在配置';最简单的';node.js+socket.IO+Express服务器

但我没有找到解决问题的办法。

这正是我所做的:

通过windows可执行文件安装节点,然后:

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);

这是完整的app.js文件。

我还尝试复制和粘贴Node http serverUsing with Express 3/4的示例,或者socket.io文档中详细介绍的app.js,但它们在调用npm start时都会引发错误。

然后,在我看来,我正试图以这种方式加载socket.io:

<script type="text/javascript" src="/socket.io/socket.io.js"></script>

据我所知,node.js应该自动获得它,但在我的情况下没有。为什么?Express版本:4.2.0

这是错误页面:

未找到
404
错误:找不到
位于Layer.app.use.res.render.message[作为句柄](C:''inetpub''wwwroot''node6''app.js:29:15)
在trim_preix(C:''inetpub''wwwroot''node6''node_modules''express''lib''router''index.js:240:15)
位于C:''inetpub''wwwroot''node6''node_modules''express''lib''router''index.js:208:9
位于Function.proto.prrocess_params(C:''inetpub''wwwroot''node6''node_modules''express''lib''router''index.js:269:12)
在next(C:''inetpub''wwwroot''node6''node_modules''express''lib''router''index.js:199:19)
在next(C:''inetpub''wwwroot''node6''node_modules''express''lib''router''index.js:176:38)
位于C:''inetpub''wwwroot''node6''node_modules''express''lib''router''index.js:137:5
位于C:''inetpub''wwwroot''node6''node_modules''express''lib''router''index.js:250:10
在next(C:''inetpub''wwwroot''node6''node_modules''express''lib''router''index.js:160:14)
下一个(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'