Why does specifying the URL as '0.0.0.0' allow my NestJS-fastify app to deploy to Heroku?

6.1k Views Asked by At

When I was trying to deploy my NestJS app to Heroku I was seeing the error Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

Why does setting the url to 0.0.0.0 make it work?

const port = process.env.PORT || 5000;
await app.listen(port, '0.0.0.0');

Context:

  • When I deploy without 0.0.0.0 my app says Listening at http://127.0.0.1:<randomNumber>
  • When I deploy with 0.0.0.0 my app says Listening at http://127.0.0.1:<randomNumber>
  • Node version: 16.13.0
  • NestJS
  • Middleware: Fastify
  • Set NPM_CONFIG_PROD=false
  • No cors
  • No helmet
"@nestjs/common": "^8.0.0",
"@nestjs/core": "^8.0.0",
"@nestjs/platform-fastify": "^8.0.11",
1

There are 1 best solutions below

0
On BEST ANSWER

127.0.0.1 - also commonly known as localhost - is used as a Loopback mechanism which allows the service to listen on the local machine (i.e. your laptop / development server) without needing the internet or even a network interface.

Since this 127.0.0.1 interface is local and internal to the dyno only, Heroku can't connect to it from the network routers that are used to send requests to your dyno; it expects an external, internet-facing network interface. This is where 0.0.0.0 comes into play. It means that the service should listen for requests on all network interfaces, which includes the one(s) that Heroku expects to send requests over. This means that Heroku can use one of those external-facing network interfaces to actually connect & route to your service.

For more information, check out this thread.