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

时间:2021-09-07 07:25:48

I followed the online chat tutorial for socket.io where you make a chat application. In my app.js file (I used the express generator) I have...

我按照socket.io的在线聊天教程进行聊天应用程序。在我的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

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. I have two questions...

当我在本地运行应用程序时套接字应用程序工作。但是为了部署它,我使用了heroku / node buildpack,当我的应用程序部署时,它在端口80上运行,例如在app.heroku.com上。我有两个问题......

1) How come my app is on a different port when deployed? (where did this happen?) 2) How do I set my page up to always look at the correct url and port?

1)部署后我的应用程序如何在不同的端口上? (这是怎么发生的?)2)如何设置我的页面以始终查看正确的URL和端口?

2 个解决方案

#1


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). So, by specifying localhost:3000, you're dooming socket.io to not be able to find its own server.

当你在Heroku上托管时,你没有连接到“localhost:3000”(而是yourapp.herokuapp.com:80,或者customdomain.com:80)。因此,通过指定localhost:3000,您将无法找到自己的服务器。

#2


You should be connecting to port 80 when running through Heroku. If you want the local and Heroku implementations to both work with your client code, then change this:

在通过Heroku运行时,您应该连接到端口80。如果您希望本地和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上运行的权限。然后,您的客户端始终可以在本地或通过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. 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.

但是,当您在端口80*问您的特定域时,运行您的进程的服务器前面的某种网络基础结构(可能是某种路由器或负载平衡器)会将您域上的端口80请求重定向到在您的进程正在运行的实际服务器上的port process.env.PORT请求。因此,发生了两件事。 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.

然后,客户端将只连接到网页加载的任何端口(自动使用相同的源),这几乎总是你想要的。

#1


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). So, by specifying localhost:3000, you're dooming socket.io to not be able to find its own server.

当你在Heroku上托管时,你没有连接到“localhost:3000”(而是yourapp.herokuapp.com:80,或者customdomain.com:80)。因此,通过指定localhost:3000,您将无法找到自己的服务器。

#2


You should be connecting to port 80 when running through Heroku. If you want the local and Heroku implementations to both work with your client code, then change this:

在通过Heroku运行时,您应该连接到端口80。如果您希望本地和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上运行的权限。然后,您的客户端始终可以在本地或通过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. 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.

但是,当您在端口80*问您的特定域时,运行您的进程的服务器前面的某种网络基础结构(可能是某种路由器或负载平衡器)会将您域上的端口80请求重定向到在您的进程正在运行的实际服务器上的port process.env.PORT请求。因此,发生了两件事。 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.

然后,客户端将只连接到网页加载的任何端口(自动使用相同的源),这几乎总是你想要的。