Node.js/socket.io/socket.io.js 未找到 express 4.0 [英] Node.js /socket.io/socket.io.js not found express 4.0

查看:25
本文介绍了Node.js/socket.io/socket.io.js 未找到 express 4.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图在我的网站上进行聊天,当我在本地进行测试时,它运行良好,因为我的本地主机上的端口 8080 可用以及所有这些好东西.但是现在我将我的代码推送到我的 Heroku 应用程序,当我尝试加载我的聊天页面时,我收到错误消息,指出它无法获取 localhost:8080/socket.io/socket.io.js.

So I'm trying to get chat working on my website, and when I was testing locally it worked great, because port 8080 on my localhost was available and all that good stuff. But now I pushed my code to my Heroku app, and when I try and load my chat page, I get the error stating that it can't get localhost:8080/socket.io/socket.io.js.

我见过 ​​node.js/socket.io找不到/socket.io.js并尝试了这些建议,但没有任何效果,甚至将 socket.io.js 文件移动到资源文件中也不起作用.我猜这是因为我使用的是 express 4.0?

I've seen node.js /socket.io/socket.io.js not found and tried the suggestions, but none worked, even moving the socket.io.js file into a resource file did not work. I'm guessing this is because I'm using express 4.0?

任何帮助将不胜感激谢谢

Any help would be appreciated Thanks

所以要添加更多细节,因为我的问题似乎有点模糊,这是我的相关 app.js 代码:

So to add more details, since my question could seem a little vague, here is my relevant app.js code:

 var client = require('socket.io').listen(8080).sockets;

在我的聊天页面的玉文件中,我有:

In my jade file for the chat page, I have:

 script (src = `'http://localhost:8080/socket.io/socket.io.js`')

以后

 var socket = io.connect(`'http://localhost:8080`');

所有这些都在本地主机上运行(我在端口 5000 上加载,socket.io 连接到端口 8080).我使用 heroku 工具带的工头开始"来做到这一点.

and all this works on localhost (I load up on port 5000, socket.io is connected to port 8080). I do this using 'foreman start' with the heroku toolbelt.

当我尝试将这些更改为在 heroku 上工作时,它坏了,我不知道如何修复它.我希望这可以澄清这个问题.

When I try and change these to work on heroku, it breaks and I'm not sure how to fix it. I hope this clarifies the question a bit.

我在跑:快递4.0.0

Edit 2: I'm running: express 4.0.0

socket.io 0.9.16

socket.io 0.9.16

节点 0.10.x

谢谢

推荐答案

你在 express 中是否有一个显式路由可以捕获所有其他路由?大概是这样的:

Do you have an explicit route in express which catches all other routes? Something like this perhaps:

app.get("/", handlers.home);
app.get("/..." ...);
...
app.get("*", handlers.error);

这可能会使 socket.io 无法为客户端托管它自己的 js 文件.有一种简单的方法可以解决此问题,因为您可能已经在 express 中设置了 publicstatic 文件夹.类似的东西:

This might keep socket.io from being able to host it's own js file for the client. There is an easy way to fix this, since you probably already have a public or static folder setup in express. Something like:

app.use(express.static("public"));

app.use(express.static("public"));

创建一个名为 socket.io 的新文件夹,并将相应的 socket.io.js 文件复制到该文件夹​​中,一切都应该没问题.但是请注意,有两个名为 socket.io.js 的文件!! 所以,如果你看到类似 "Uncaught ReferenceError: require is not defined" 的内容代码>这意味着您复制了node-ey"服务器端文件.这是要复制的正确客户端文件:

Make a new folder called socket.io and copy over the appropriate socket.io.js file into said folder, and all should be well. However note that there are two files named socket.io.js!! So, if you see something like "Uncaught ReferenceError: require is not defined" it means you copied the "node-ey" server side file. Here is the correct client file to copy:

app_dir/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.min.js

app_dir/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.min.js

请注意@BHendricks:我本来想回复您的评论,但我目前缺乏所需的声誉.

Note @BHendricks: I would have just posted as a reply to your comment, but I currently lack the required reputation.

编辑:OP 问题可能更多地与本地主机"问题有关.当从客户端(比如您的家庭 IP)连接时,就您的浏览器而言 - localhost 意味着与本地托管内容的机器的连接.由于您的家用机器(或手机)没有托管 socket.io,所以这是失败的.

Edit: The OPs question probably has more to do with the "localhost" issue. When connecting from a client (say your home IP), as far as your browser knows - localhost implies a connection with the machine which is locally hosting stuff. Since your home machine (or phone) does not host socket.io, this is failing.

您需要做的是让您的服务器嵌入套接字连接信息(完全限定的主机名、IP 等).这可以在服务器通过客户端连接呈现"页面时完成.

What you need to do is have your server embed the socket connection information (either a fully qualified hostname, ip etc). This can be done when the server "renders" the page with the client connection.

这篇关于Node.js/socket.io/socket.io.js 未找到 express 4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆