简体   繁体   English

部署到heroku时,如何使我的node / socket.io应用使用正确的端口?

[英]How do I get my node/socket.io app to use the correct port when deployed to heroku?

I followed the online chat tutorial for socket.io where you make a chat application. 我遵循了socket.io的在线聊天教程,在其中创建了聊天应用程序。 In my app.js file (I used the express generator) I have... 在我的app.js文件(我使用快递生成器)中,我有...

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

This means I need to browse to (and thus my socket server is running from)... 这意味着我需要浏览到(因此我的套接字服务器正在运行)...

http://localhost:3000 HTTP://本地主机:3000

On my page I have the lines... 我的页面上有行...

  var socket = io.connect('http://localhost:3000');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });

When I run the application locally the socket application works. 当我在本地运行该应用程序时,套接字应用程序将运行。 However to deploy it I used the heroku/node buildpack and when my app is deployed it is running on port 80, so for example is on app.heroku.com. 但是,要部署它,我使用了heroku / node buildpack,当部署我的应用程序时,它运行在端口80上,例如,在app.heroku.com上。 I have two questions... 我有两个问题

1) How come my app is on a different port when deployed? 1)部署后,我的应用程序为何位于不同的端口上? (where did this happen?) 2) How do I set my page up to always look at the correct url and port? (这发生在什么地方?)2)如何设置页面以始终查看正确的URL和端口?

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

This is fine. 这可以。 Keep in mind that this does nothing, it's just an arbitrarily set value in your app. 请记住,这什么也没做,只是您应用中的任意设置值。 In order to listen on that port, you must later tell your server to listen, like app.listen(app.get('port')) . 为了监听该端口,您稍后必须告诉服务器监听,例如app.listen(app.get('port'))

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

This is what you want to change, to: 您要更改为:

var socket = io.connect();

When you're hosting on Heroku, you aren't connecting to "localhost:3000" (but rather yourapp.herokuapp.com:80, or customdomain.com:80). 在Heroku上托管时,您未连接到“ localhost:3000”(而是yourapp.herokuapp.com:80或customdomain.com:80)。 So, by specifying localhost:3000, you're dooming socket.io to not be able to find its own server. 因此,通过指定localhost:3000,您注定socket.io无法找到自己的服务器。

You should be connecting to port 80 when running through Heroku. 通过Heroku运行时,您应该连接到端口80。 If you want the local and Heroku implementations to both work with your client code, then change this: 如果您希望本地和Heroku实现都可以与您的客户端代码一起使用,请更改此设置:

var port = normalizePort(process.env.PORT || '3000');

to this: 对此:

var port = normalizePort(process.env.PORT || '80');

And, make sure that your local installation has privileges to run on port 80. Then, your client can always access it on port 80 either locally or through Heroku. 并且,请确保您的本地安装具有在端口80上运行的特权。然后,您的客户端始终可以在本地80上或通过Heroku在端口80上对其进行访问。

Or, if it's inconvenient to run locally on port 80 for privilege reasons, then you can leave the port 3000 there for the local installation and configure your local iptable on your local server so that port 80 requests are automatically forwarded to port 3000 where the server process is. 或者,如果出于特权原因而无法在端口80上本地运行,则可以将端口3000留在本地进行本地安装,并在本地服务器上配置本地iptable,以便将端口80请求自动转发到服务器的端口3000过程是。


Heroku uses a shared infrastructure, running many different processes on the same server with each one running on its own port as specified by process.env.PORT that is separately configured for each separate server process. Heroku使用共享的基础结构,在同一服务器上运行许多不同的进程,每个进程都在由process.env.PORT指定的端口上运行,该端口是为每个单独的服务器进程分别配置的。

But, when your specific domain is accessed on port 80, some sort of network infrastructure out in front of the server that your process is run on (probably some sort of router or load balancer) will redirect that port 80 request on your domain to a port process.env.PORT request on the actual server that your process is running on. 但是,当通过端口80访问您的特定域时,运行您的进程的服务器前面的某种网络基础结构(可能是某种路由器或负载平衡器)会将您域上的该端口80请求重定向到运行进程的实际服务器上的port process.env.PORT请求。 Thus, two things happen. 因此,发生了两件事。 1) Multiple server processes can be run on the same server box each on their own unique port and 2) Each separate domain can be accessed on port 80 and that port will be automatically forwarded to whatever port the process is actually running on the actual server box. 1)多个服务器进程可以在同一服务器盒上运行,每个服务器进程都在各自的唯一端口上,并且2)每个单独的域都可以在端口80上访问,并且该端口将自动转发到进程在实际服务器上实际运行的任何端口框。


Incidentally, if you change the client code to this: 顺便说一句,如果您将客户端代码更改为此:

io.connect();

Then, the client will just connect to whatever port the web page was loaded from (automatically use the same origin) which is pretty much always what you want. 然后,客户端将只连接到从中加载网页的任何端口(自动使用相同的来源),这几乎总是您想要的端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM