It is the sequence of events from starting the app, running it, to shutting it down, including hooks to run code at each stage.
Click to reveal answer
beginner
Name the lifecycle hook used to run code right after the application has fully started.
The hook is called onApplicationBootstrap().
Click to reveal answer
intermediate
What is the purpose of the onModuleDestroy() hook in NestJS?
It allows you to run cleanup code when a module is about to be destroyed, like closing database connections.
Click to reveal answer
intermediate
How does NestJS handle graceful shutdown of an application?
NestJS listens for shutdown signals and calls lifecycle hooks like onApplicationShutdown() to clean resources before exit.
Click to reveal answer
intermediate
Which interface should a service implement to run code when the application is shutting down?
Implement the OnApplicationShutdown interface and define the onApplicationShutdown() method.
Click to reveal answer
Which lifecycle hook runs after a module is initialized in NestJS?
AonModuleInit()
BonApplicationShutdown()
ConModuleDestroy()
DbeforeApplicationStart()
✗ Incorrect
onModuleInit() runs after a module is initialized.
What does the onApplicationBootstrap() hook do?
ARuns code after the entire application has bootstrapped
BRuns code during shutdown
CRuns code when a module is destroyed
DRuns code before the app starts
✗ Incorrect
onApplicationBootstrap() runs after the whole app is ready.
Which interface is used to handle cleanup during app shutdown?
AOnModuleInit
BOnApplicationShutdown
COnModuleDestroy
DOnBootstrap
✗ Incorrect
OnApplicationShutdown is for cleanup during shutdown.
What signal does NestJS listen for to trigger graceful shutdown?
ASIGBOOT
BSIGSTART
CSIGRUN
DSIGINT
✗ Incorrect
NestJS listens for SIGINT to start shutdown.
Which lifecycle hook is NOT part of NestJS application lifecycle?
AonModuleDestroy
BonApplicationShutdown
ConAppStart
DonModuleInit
✗ Incorrect
onAppStart is not a NestJS lifecycle hook.
Explain the main lifecycle hooks in NestJS and when they run.
Think about the app starting, running, and stopping phases.
You got /4 concepts.
Describe how NestJS supports graceful shutdown and why it is important.
Consider what happens when you stop a running app.
You got /4 concepts.
Practice
(1/5)
1. Which lifecycle hook in NestJS is used to run code right after a module has been initialized?
easy
A. OnModuleInit
B. OnApplicationBootstrap
C. OnApplicationShutdown
D. OnModuleDestroy
Solution
Step 1: Understand module initialization timing
The OnModuleInit hook runs immediately after the module's dependencies are resolved and the module is set up.
Step 2: Match the hook to the description
OnApplicationBootstrap runs after the entire app starts, OnApplicationShutdown runs before app stops, and OnModuleDestroy runs before module is destroyed.
Final Answer:
OnModuleInit -> Option A
Quick Check:
Run code after module setup = OnModuleInit [OK]
Hint: Module init code runs in OnModuleInit hook [OK]
Common Mistakes:
Confusing OnModuleInit with OnApplicationBootstrap
Using OnApplicationShutdown for startup code
Thinking OnModuleDestroy runs after module init
2. Which of the following is the correct way to implement the OnApplicationShutdown lifecycle hook in a NestJS service?
easy
A. export class MyService implements OnApplicationShutdown { onApplicationShutdown(signal?: string) { /* code */ } }
B. export class MyService implements OnModuleInit { onApplicationShutdown() { /* code */ } }
C. export class MyService implements OnApplicationBootstrap { onApplicationShutdown(signal: number) { /* code */ } }
D. export class MyService implements OnModuleDestroy { onApplicationShutdown(signal?: string) { /* code */ } }
Solution
Step 1: Identify correct interface and method signature
The OnApplicationShutdown interface requires implementing onApplicationShutdown(signal?: string) method.
Step 2: Check method name and parameter types
export class MyService implements OnApplicationShutdown { onApplicationShutdown(signal?: string) { /* code */ } } correctly implements OnApplicationShutdown with optional string parameter signal. Others mismatch interface or method name.
Final Answer:
export class MyService implements OnApplicationShutdown { onApplicationShutdown(signal?: string) { /* code */ } } -> Option A
A. Should implement OnModuleDestroy instead of OnApplicationShutdown
B. Wrong method name; should be onShutdown instead
C. Missing optional signal parameter in onApplicationShutdown method
D. No error; code is correct
Solution
Step 1: Check method signature for OnApplicationShutdown
The interface requires onApplicationShutdown(signal?: string) with an optional string parameter.
Step 2: Identify missing parameter in method
The method lacks the optional signal parameter, causing a signature mismatch error.
Final Answer:
Missing optional signal parameter in onApplicationShutdown method -> Option C
Quick Check:
Method signature must match interface including optional parameters [OK]
Hint: Include optional signal parameter in onApplicationShutdown method [OK]
Common Mistakes:
Omitting optional signal parameter
Using wrong method name
Confusing OnApplicationShutdown with OnModuleDestroy
5. You want to perform cleanup tasks only when the app shuts down due to a SIGTERM signal, ignoring other signals. How should you implement this in a NestJS service?
hard
A. Override the main bootstrap function to handle SIGTERM manually
B. Implement OnApplicationShutdown and check if signal === 'SIGTERM' inside onApplicationShutdown method
C. Use OnApplicationBootstrap to detect shutdown signals
D. Implement OnModuleDestroy and check for SIGTERM signal there
Solution
Step 1: Choose correct lifecycle hook for app shutdown
OnApplicationShutdown runs when the app is stopping and receives the shutdown signal.
Step 2: Filter signal inside onApplicationShutdown method
Inside onApplicationShutdown(signal?: string), check if signal === 'SIGTERM' to run cleanup only for that signal.
Final Answer:
Implement OnApplicationShutdown and check if signal === 'SIGTERM' inside onApplicationShutdown method -> Option B
Quick Check:
Filter shutdown signal inside OnApplicationShutdown hook [OK]
Hint: Check signal inside OnApplicationShutdown to filter SIGTERM [OK]
Common Mistakes:
Using OnModuleDestroy which runs on module destroy, not app shutdown
Trying to detect shutdown in OnApplicationBootstrap
Manually overriding bootstrap instead of lifecycle hooks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.