Discover how to catch app problems early with just a few lines of code!
Why Health checks (Terminus) in NestJS? - Purpose & Use Cases
Imagine you run a web app and want to know if your database or external services are working fine. You try to check each service by writing separate code everywhere.
Manually checking each service is slow, messy, and easy to forget. If one service fails, you might not notice quickly, causing your app to break without warning.
Health checks with Terminus in NestJS give you a simple, unified way to monitor all parts of your app automatically and report their status clearly.
if (db.isConnected()) { console.log('DB OK'); } else { console.log('DB Down'); }
@HealthCheck()
check() {
return this.health.check([
() => this.db.pingCheck('database'),
]);
}You can easily see your app's health in one place and react fast to problems before users notice.
A company uses Terminus health checks to monitor their payment service and database, so if either fails, their alert system notifies the team immediately.
Manual health checks are slow and error-prone.
Terminus provides automatic, clear health monitoring.
This helps keep apps reliable and users happy.
Practice
Solution
Step 1: Understand Terminus role in NestJS
Terminus is a tool designed to add health checks to monitor app status.Step 2: Compare options with Terminus purpose
Only To add health checks that monitor if the app and its parts are working well describes health checks, which is Terminus's main use.Final Answer:
To add health checks that monitor if the app and its parts are working well -> Option BQuick Check:
Terminus = Health checks [OK]
- Confusing Terminus with authentication libraries
- Thinking Terminus manages database tasks
- Assuming Terminus improves performance directly
HealthCheckService in a NestJS Terminus health check controller?Solution
Step 1: Recall NestJS dependency injection syntax
Use constructor with private readonly and type for proper injection.Step 2: Check each option's correctness
constructor(private readonly healthCheckService: HealthCheckService) {} uses private readonly and type, which is correct. Others miss readonly, type, or access modifier.Final Answer:
constructor(private readonly healthCheckService: HealthCheckService) {} -> Option CQuick Check:
Inject with private readonly and type [OK]
- Omitting access modifier (private/public)
- Not specifying the type HealthCheckService
- Using public instead of private readonly
async check() {
return this.healthCheckService.check([
() => this.dbHealthIndicator.pingCheck('database')
]);
}Solution
Step 1: Understand healthCheckService.check behavior
It returns 200 OK with status 'ok' if all checks pass.Step 2: Analyze the database pingCheck result
If database is healthy, pingCheck passes, so overall status is 'ok'.Final Answer:
200 OK with status 'ok' and details about database -> Option AQuick Check:
Healthy check = 200 OK [OK]
- Assuming failure status on healthy database
- Confusing route existence with health check response
- Expecting redirects from health checks
@Controller('health')
export class HealthController {
constructor(private readonly healthCheckService: HealthCheckService) {}
@Get()
async check() {
return this.healthCheckService.check([
this.httpHealthIndicator.pingCheck('nestjs-docs', 'https://docs.nestjs.com')
]);
}
}Solution
Step 1: Check usage of httpHealthIndicator
httpHealthIndicator is used but not injected or declared anywhere.Step 2: Verify constructor parameters
Only healthCheckService is injected; httpHealthIndicator is missing.Final Answer:
Missing injection of HttpHealthIndicator in constructor -> Option AQuick Check:
Inject all indicators used [OK]
- Forgetting to inject HttpHealthIndicator
- Using wrong HTTP method for health checks
- Assuming decorators need no path
Solution
Step 1: Understand combining multiple checks
healthCheckService.check accepts an array of async functions for multiple checks.Step 2: Verify correct usage of indicators
Use dbHealthIndicator.pingCheck and httpHealthIndicator.pingCheck as separate functions in the array.Step 3: Evaluate other options
Separate controllers or nesting checks is incorrect; ignoring API check misses requirement.Final Answer:
Use healthCheckService.check with an array of functions calling dbHealthIndicator.pingCheck and httpHealthIndicator.pingCheck -> Option DQuick Check:
Combine checks in array for one endpoint [OK]
- Splitting checks into multiple controllers unnecessarily
- Nesting one check inside another incorrectly
- Ignoring one of the required checks
Ready to prove it?
Pass the test and this pattern moves unknown
