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

查看:197
本文介绍了没有找到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`');

所有这些都在localhost上工作(我在5000端口上加载,socket.io连接到端口8080)。我用'工头开始'用英雄工具带做这个。

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.

编辑2:
我正在运行:
express 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文件。有一个简单的方法来解决这个问题,因为你可能已经有一个 public static 如下:

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 !! 所以,如果你看到类似未捕获ReferenceError:require未定义表示您复制了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.

修改
OPs问题可能有更多的是与localhost问题。当从客户端(例如您的家庭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天全站免登陆