Nestjs Request and Application Lifecycle

What is the order of execution of the following processes in a request, for a route that implements: middleware, pipes, guards, interceptors, and any other potential request process

The common order is:

  • Middlewares
  • Guards
  • Interceptors (before the stream is manipulated)
  • Pipes
  • Interceptors (after the stream is manipulated)
  • Exception filters (if any exception is caught)

What is the lifespan of modules and providers in a NestJS application? Do they last for the lifespan of a request, or the application, or something else?

They do last for the lifespan of the application. Modules are destroyed when a NestApplication or a NestMicroservice is being closed (see close method from INestApplication).

Are there any lifecycle hooks, in addition to OnModuleInit and OnModuleDestroy?

No there aren't at the moment.

What causes a Modeule to be destroyed (and trigger the OnModuleDestroy event)?

See my answer to the second point. As you look interested in lifecyle hooks, you might pay some interest to issues #938 and #550

Tags:

Nestjs