Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nestjs Request and Application Lifecycle

Tags:

nestjs

I am looking for information about the request and application life-cycle for The NestJS framework. Specifically:

  1. 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

  2. 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?

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

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

like image 688
arhnee Avatar asked Aug 06 '18 12:08

arhnee


1 Answers

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

like image 199
VinceOPS Avatar answered Oct 13 '22 12:10

VinceOPS