0
0
NestJSframework~10 mins

Application lifecycle in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to implement the OnModuleInit lifecycle hook in a NestJS service.

NestJS
import { Injectable, [1] } from '@nestjs/common';

@Injectable()
export class AppService implements [1] {
  onModuleInit() {
    console.log('Module initialized');
  }
}
Drag options to blanks, or click blank then click option'
AModuleInit
BOnInit
COnModuleInit
DInitModule
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent interface like OnInit or InitModule.
Forgetting to import the interface from '@nestjs/common'.
2fill in blank
medium

Complete the code to implement the OnApplicationBootstrap lifecycle hook in a NestJS service.

NestJS
import { Injectable, OnApplicationBootstrap } from '@nestjs/common';

@Injectable()
export class AppService implements [1] {
  onApplicationBootstrap() {
    console.log('Application bootstrap completed');
  }
}
Drag options to blanks, or click blank then click option'
AOnModuleDestroy
BOnApplicationBootstrap
COnBootstrap
DOnAppStart
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing with OnModuleDestroy or other lifecycle hooks.
Using incorrect interface names like OnBootstrap.
3fill in blank
hard

Fix the error in the code by completing the lifecycle hook interface for graceful shutdown.

NestJS
import { Injectable, [1] } from '@nestjs/common';

@Injectable()
export class AppService implements [1] {
  async onModuleDestroy() {
    console.log('Module is being destroyed');
  }
}
Drag options to blanks, or click blank then click option'
AOnModuleDestroy
BOnDestroy
COnAppDestroy
DOnDestroyModule
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect interface names like OnDestroy or OnAppDestroy.
Not implementing the interface but defining the method.
4fill in blank
hard

Fill both blanks to implement the OnApplicationShutdown lifecycle hook and handle shutdown logic.

NestJS
import { Injectable, [1] } from '@nestjs/common';

@Injectable()
export class AppService implements [2] {
  onApplicationShutdown(signal?: string) {
    console.log(`Application shutting down due to: ${signal}`);
  }
}
Drag options to blanks, or click blank then click option'
AOnApplicationShutdown
BOnShutdown
COnAppShutdown
DOnShutdownApplication
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for import and implementation.
Using incorrect interface names like OnShutdown or OnAppShutdown.
5fill in blank
hard

Fill all three blanks to implement OnModuleInit, OnApplicationBootstrap, and OnModuleDestroy lifecycle hooks in a NestJS service.

NestJS
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');
  }
}
Drag options to blanks, or click blank then click option'
AOnModuleInit
BOnApplicationBootstrap
COnModuleDestroy
DOnApplicationShutdown
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up interface names or missing one of them.
Using OnApplicationShutdown instead of OnModuleDestroy for module destruction.