Complete the code to implement the OnModuleInit lifecycle hook in a NestJS service.
import { Injectable, [1] } from '@nestjs/common'; @Injectable() export class AppService implements [1] { onModuleInit() { console.log('Module initialized'); } }
The OnModuleInit interface is used to hook into the module initialization lifecycle event in NestJS.
Complete the code to implement the OnApplicationBootstrap lifecycle hook in a NestJS service.
import { Injectable, OnApplicationBootstrap } from '@nestjs/common'; @Injectable() export class AppService implements [1] { onApplicationBootstrap() { console.log('Application bootstrap completed'); } }
The OnApplicationBootstrap interface allows running code after all modules have been initialized and the application is bootstrapped.
Fix the error in the code by completing the lifecycle hook interface for graceful shutdown.
import { Injectable, [1] } from '@nestjs/common'; @Injectable() export class AppService implements [1] { async onModuleDestroy() { console.log('Module is being destroyed'); } }
The OnModuleDestroy interface is the correct lifecycle hook for cleanup when a module is destroyed.
Fill both blanks to implement the OnApplicationShutdown lifecycle hook and handle shutdown logic.
import { Injectable, [1] } from '@nestjs/common'; @Injectable() export class AppService implements [2] { onApplicationShutdown(signal?: string) { console.log(`Application shutting down due to: ${signal}`); } }
The OnApplicationShutdown interface is used to run code during application shutdown, such as cleanup tasks.
Fill all three blanks to implement OnModuleInit, OnApplicationBootstrap, and OnModuleDestroy lifecycle hooks in a NestJS service.
import { Injectable, [1], [2], [3] } from '@nestjs/common'; @Injectable() export class AppService implements [1], [2], [3] { onModuleInit() { console.log('Module initialized'); } onApplicationBootstrap() { console.log('Application bootstrap completed'); } onModuleDestroy() { console.log('Module destroyed'); } }
These three interfaces correspond to key lifecycle events: module initialization, application bootstrap, and module destruction.