0

In the main.ts file in Nest JS, the following bootstrap function contains the app.listen() method.

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
} 

My question is what is the actual use of the following parameters:

  • hostname: string,
  • callback?: () => void

specially that the listenAsync is deprecated and the current .listen() method can be awaited, so why will I ever use the callback that is executed when the server is running.

  • look at the source here: https://github.com/nestjs/nest/blob/6363cbe29d131f54361adf6e0a3367253345b255/packages/platform-express/adapters/express-adapter.ts#L132-L134 – Micael Levi Aug 09 '22 at 19:57
  • @MicaelLevi, I looked at it, but still can't understand when to use the callback... – abu mo2ayad Aug 10 '22 at 04:44

1 Answers1

0

If you are using the fastify adapter and docker, you need to specify the host as 0.0.0.0 so that docker and fastify can communicate properly. Otherwise it'll only run on 127.0.0.1 which isn't quite right. As for the callback, sometimes fastify can throw an exception that isn't bubbled up completely, so the optional callback can help with debugging what is being thrown

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
  • Thanks, the first point is clear, but regarding the second point ... isn't this callback triggered when the server is successfully listening? and it has no parameters + it is void ... I think that it's there because the old listenAsync is deprecated. – abu mo2ayad Aug 10 '22 at 04:39
  • The callback does have parameters [you can see it being called here](https://github.com/nestjs/nest/blob/6363cbe29d131f54361adf6e0a3367253345b255/packages/core/nest-application.ts#L272), but generally Nest has tried to make it to where you shouldn't need to worry about it – Jay McDoniel Aug 10 '22 at 13:24