找不到Socket.io.js(node.js + express + socket.io) [英] Socket.io.js not found (node.js + express + socket.io)
问题描述
我一直在检查其他相关答案,例如:
I've been checking other related answers such as:
- node.js/socket.io/socket.io找不到.js
- Socket.io未由Node.js服务器提供
- 在客户端上找不到socket.io.js
- 配置最简单的" node.js + socket.IO + Express服务器
- 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
但是我没有找到解决问题的方法.
But I didn't find the solution to my problem.
这正是我所做的:
通过Windows可执行文件安装节点,然后:
Installed node through the windows executable and then:
express node6 --hogan --ejs
cd node6
npm install
npm install socket.io
npm start
我的app.js是默认应用程序,但是我添加了以下几行将其与socket.io链接:
My app.js is the default one but I added the following lines to link it with socket.io:
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io')(server);
io.listen(http);
http.listen(3000);
我还尝试复制并粘贴 Node http server 和使用Express 3/4 或详细的 ,但是当调用npm start
时,它们都抛出错误.
I also tried to copy and paste the examples for Node http server and Using with Express 3/4 or the app.js detailed in the socket.io docs, but both of them throw errors when calling npm start
.
然后,在我的Hoggn视图中,我试图以这种方式加载socket.io:
Then, in my hoggn view, I'm trying to load socket.io this way:
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
据我所知,node.js应该自动获取它,但就我而言不是.为什么? 速成版:4.2.0
As far as I know, node.js should get it automatically, but it doesn't in my case. Why? Express version: 4.2.0
这是错误页面:
未找到
404
错误:未找到
在Layer.app.use.res.render.message [作为句柄](C:\ inetpub \ wwwroot \ node6 \ app.js:29:15)
在trim_prefix(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.process_params(C:\ inetpub \ wwwroot \ node6 \ node_modules \ express \ lib \ router \ index.js:269:12)
在下一个(C:\ inetpub \ wwwroot \ node6 \ node_modules \ express \ lib \ router \ index.js:199:19)
在下一个(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
在下一个(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)
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知道它在那里.
Instead of doing npm install socket.io
you have to do npm install socket.io --save
so 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.
然后将源路径从'/socket.io/socket.io.js'
更改为'http://' + location.hostname + ':3000/socket.io/socket.io.js'
.
Then change your source path from '/socket.io/socket.io.js'
to 'http://' + location.hostname + ':3000/socket.io/socket.io.js'
.
这篇关于找不到Socket.io.js(node.js + express + socket.io)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!