Issue with NestJS and Socket.IO app deployment on Heroku (PORT)

huangapple go评论46阅读模式
英文:

Issue with NestJS and Socket.IO app deployment on Heroku (PORT)

问题

我使用NestJS和Socket.IO创建了一个应用程序。我使用了一个网关来使其监听除应用程序默认端口以外的端口。当我在本地主机上运行项目时,它运行正常。然而,当我部署到Heroku时,它不会响应我指定的端口。我还尝试让它监听与应用程序相同的端口,但它记录了一个404错误。你能帮助我解决这些问题吗?

英文:

I have created an app using NestJS and Socket.IO. I used a gateway to make it listen on a port other than the app's default port. When I run the project on my localhost, it works fine. However, when I deploy it to Heroku, it doesn't respond on the port I specified. I also tried making it listen on the same port as the app, but it logged a 404 error.

Can you help me resolve these issue?

答案1

得分: 1

如果有类似问题的人:

你需要知道一件事 - Heroku 仅为你的应用提供一个可用端口,并由 Heroku 定义它(你只需使用 process.env.PORT)。
因此,我们将套接字和 REST API 放在同一个端口上。

为此,我们只需为 HTTP 服务器指定端口。

通常情况下,每个网关都在与 HTTP 服务器相同的端口上进行侦听,除非你的应用程序不是 Web 应用程序,或者你已手动更改端口。可以通过向 @WebSocketGateway(80) 装饰器传递参数来修改此默认行为,其中 80 是选择的端口号。

HTTP:

app.listen(process.env.PORT)

WSS:

@WebSocketGateway({ cors: true, })
英文:

If someone has a similar issue:

You need to know one thing - only one port is available for your application from Heroku, and Heroku defines it (you only need to use process.env.PORT).
Therefore, we put sockets and rest API on the same port.

To do this, we need to specify the port only for the HTTP server.

> In general, each gateway is listening on the same port as the HTTP server, unless your app is not a web application, or you have changed the port manually. This default behavior can be modified by passing an argument to the @WebSocketGateway(80) decorator where 80 is a chosen port number.

HTTP:

app.listen(process.env.PORT)

WSS:

@WebSocketGateway({ cors: true, })

huangapple
  • 本文由 发表于 2023年4月17日 19:54:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034895.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定