📖 Scenario: You are building a simple NestJS server that needs to report its health status. This is important for monitoring and ensuring the server is running well.
🎯 Goal: Create a health check endpoint using Terminus in NestJS that reports the server is healthy.
📋 What You'll Learn
Create a health check controller
Configure Terminus health check module
Implement a basic health check returning 'ok' status
Expose the health check endpoint at '/health'
💡 Why This Matters
🌍 Real World
Health checks help monitor if your server and its dependencies are working well. This is important for uptime and reliability.
💼 Career
Many companies use health checks in their backend services to integrate with monitoring tools and orchestrators like Kubernetes.
Progress0 / 4 steps
1
Setup Health Check Controller
Create a controller called HealthController with a route /health that will later return health status.
NestJS
Hint
Use @Controller('health') to set the route and @Get() for the GET method.
2
Add Terminus Health Module
Import TerminusModule from @nestjs/terminus and add it to the imports array of your main application module called AppModule.
NestJS
Hint
Remember to import TerminusModule and add it to the imports array in @Module.
3
Use Terminus Health Check Service
Inject HealthCheckService and HttpHealthIndicator into HealthController. Use HealthCheckService.check() with HttpHealthIndicator.pingCheck('nestjs-docs', 'https://docs.nestjs.com') inside the check() method to perform a real health check.
NestJS
Hint
Use dependency injection in the constructor and call this.health.check() with this.http.pingCheck() inside the check() method.
4
Complete and Test Health Endpoint
Ensure the HealthController and AppModule are correctly set up with Terminus. The health check endpoint should be accessible at /health and return JSON with the status of the ping check.
NestJS
Hint
Review all previous steps and ensure the health check endpoint is fully implemented and the module imports are correct.
Practice
(1/5)
1. What is the main purpose of using Terminus in a NestJS application?
easy
A. To handle user authentication and authorization
B. To add health checks that monitor if the app and its parts are working well
C. To manage database migrations automatically
D. To optimize the app's performance by caching responses
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 B
Quick Check:
Terminus = Health checks [OK]
Hint: Terminus is about health checks, not auth or caching [OK]
Common Mistakes:
Confusing Terminus with authentication libraries
Thinking Terminus manages database tasks
Assuming Terminus improves performance directly
2. Which of the following is the correct way to inject the HealthCheckService in a NestJS Terminus health check controller?
easy
A. constructor(private healthCheckService) {}
B. constructor(public healthCheckService: HealthCheckService) {}
C. constructor(private readonly healthCheckService: HealthCheckService) {}
D. constructor(healthCheckService: HealthCheckService) {}
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 C
Quick Check:
Inject with private readonly and type [OK]
Hint: Use 'private readonly' with type for injection [OK]
Common Mistakes:
Omitting access modifier (private/public)
Not specifying the type HealthCheckService
Using public instead of private readonly
3. Given this NestJS Terminus health check code snippet, what will be the HTTP response status when the database is healthy?
A. Missing injection of HttpHealthIndicator in constructor
B. Wrong HTTP method; should use @Post instead of @Get
C. HealthCheckService is not imported
D. Controller decorator is missing a path
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 A
Quick Check:
Inject all indicators used [OK]
Hint: Inject all health indicators you use [OK]
Common Mistakes:
Forgetting to inject HttpHealthIndicator
Using wrong HTTP method for health checks
Assuming decorators need no path
5. You want to create a custom health check in NestJS Terminus that checks both database connectivity and an external API availability. Which approach correctly combines these checks in one health endpoint?
hard
A. Call dbHealthIndicator.pingCheck inside httpHealthIndicator.pingCheck callback
B. Create two separate controllers, one for database and one for API, each with its own healthCheckService
C. Use only healthCheckService.check with dbHealthIndicator.pingCheck and ignore API check
D. Use healthCheckService.check with an array of functions calling dbHealthIndicator.pingCheck and httpHealthIndicator.pingCheck
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 D
Quick Check:
Combine checks in array for one endpoint [OK]
Hint: Pass all checks as functions array to healthCheckService.check [OK]
Common Mistakes:
Splitting checks into multiple controllers unnecessarily
Nesting one check inside another incorrectly
Ignoring one of the required checks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.