【问题标题】:Socket.io.js not found (node.js + express + socket.io)未找到 Socket.io.js(node.js + express + socket.io)
【发布时间】:2014-10-20 22:06:51
【问题描述】:

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

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

这正是我所做的:

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

Here's the complete app.js file.

我还尝试复制和粘贴 Node http serverUsing with Express 3/4 的示例或 app.js 详细说明 in the socket.io docs,但两者他们在调用npm start 时会抛出错误。

然后,在我的 hoggn 视图中,我正在尝试以这种方式加载 socket.io:

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

据我所知,node.js 应该会自动获取它,但在我的情况下不会。为什么? 快捷版: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)

【问题讨论】:

  • 错误是什么?
  • 找不到文件? socket.io.js 的 404
  • @lombausch 用错误日志更新了问题。
  • 你真的在路径中有文件吗?
  • @lombausch 该路径在物理上不存在。在我见过的任何例子中也没有。

标签: javascript node.js express socket.io


【解决方案1】:

您不必执行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'

【讨论】:

  • --save updates dependencies in package.json 该模块保存在完全相同的位置,恕我直言,这与他的问题无关。
  • 看答案的第二部分,他没有正确设置socket.io源的路径。
猜你喜欢
  • 2014-06-04
  • 2013-10-25
  • 1970-01-01
  • 2013-11-06
  • 2019-01-26
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多