🦅 NestJS Beginner

What is NestJS lifecycle hooks?

Answer

NestJS provides lifecycle hooks that let you run code at specific points in the application and module lifecycle. OnModuleInit: called after the module's dependencies are resolved. Use for initialization tasks like connecting to services. OnApplicationBootstrap: called after all modules initialize and the application is ready. OnModuleDestroy: called when a module is about to be destroyed (during shutdown). BeforeApplicationShutdown: receives the shutdown signal. OnApplicationShutdown: after cleanup is done. Implement the interface in a provider: class UserService implements OnModuleInit { async onModuleInit() { await this.loadCache(); } }. Enable shutdown hooks with app.enableShutdownHooks() in main.ts.