0
0
NestJSframework~10 mins

Health checks (Terminus) in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Health checks (Terminus)
Start NestJS App
Import Terminus Module
Define Health Indicator(s)
Create Health Controller
Add /health Endpoint
Request /health
Run Health Checks
Return JSON Status
Client Receives Health Status
The app imports Terminus, defines health checks, exposes a /health endpoint, runs checks on request, and returns status.
Execution Sample
NestJS
import { Controller, Get } from '@nestjs/common';
import { HealthCheckService, TypeOrmHealthIndicator } from '@nestjs/terminus';

@Controller('health')
export class HealthController {
  constructor(
    private readonly health: HealthCheckService,
    private readonly db: TypeOrmHealthIndicator,
  ) {}

  @Get()
  async check() {
    return this.health.check([
      () => this.db.pingCheck('database'),
    ]);
  }
}
This code sets up a health check endpoint that verifies database connectivity.
Execution Table
StepActionEvaluationResult
1App starts and imports TerminusModuleTerminusModule loadedTerminus features ready
2HealthController created with HealthCheckServiceHealthCheckService injectedReady to run checks
3GET /health endpoint calledTriggers check() methodHealth checks start
4Run db.pingCheck('database')Database connection testedReturns 'up' or 'down'
5HealthCheckService aggregates resultsCombines all checksCreates JSON status
6Return JSON responseStatus sent to client{"status":"ok","info":{"database":{"status":"up"}},"error":{},"details":{"database":{"status":"up"}}}
7Client receives responseReads health statusKnows app is healthy
8If any check failsStatus changes to 'error'Client alerted of failure
💡 Execution stops after sending health status response to client
Variable Tracker
VariableStartAfter Step 4After Step 5Final
healthCheckResultundefined{"database":"up"}{"status":"ok","info":{"database":{"status":"up"}}}{"status":"ok","info":{"database":{"status":"up"}},"error":{},"details":{"database":{"status":"up"}}}
Key Moments - 3 Insights
Why does the health check return a JSON object instead of just 'ok' or 'fail'?
The health check returns detailed JSON (see execution_table step 6) to provide info on each check, not just overall status.
What happens if the database ping check fails?
If db.pingCheck fails (execution_table step 8), the status changes to 'error' and the client is informed of the failure.
Why do we inject HealthCheckService into the controller?
HealthCheckService runs the checks and aggregates results (step 2 and 5), so injecting it allows the controller to trigger health checks.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the health check result after step 4?
Aundefined
B{"status":"ok"}
C{"database":"up"}
D{"status":"error"}
💡 Hint
Check the 'Evaluation' and 'Result' columns at step 4 in the execution_table.
At which step does the client receive the health status response?
AStep 7
BStep 5
CStep 6
DStep 8
💡 Hint
Look for the step mentioning 'Client receives response' in the execution_table.
If the database ping check fails, how does the status change in the response?
AStatus remains 'ok'
BStatus changes to 'error'
CStatus becomes 'unknown'
DNo status is returned
💡 Hint
See execution_table step 8 about failure handling.
Concept Snapshot
Health checks with Terminus in NestJS:
- Import TerminusModule in your app
- Inject HealthCheckService in a controller
- Define health indicators like db.pingCheck
- Create a GET /health endpoint that runs checks
- Returns detailed JSON status showing each check
- Status 'ok' means healthy; 'error' means failure
Full Transcript
This visual execution trace shows how NestJS apps use Terminus for health checks. The app imports TerminusModule and creates a HealthController with HealthCheckService injected. When a client calls GET /health, the controller runs health indicators like database ping checks. The results are combined into a JSON response showing overall status and details. If all checks pass, status is 'ok'; if any fail, status is 'error'. The client receives this JSON to know if the app is healthy. Variables like healthCheckResult update step-by-step as checks run. Key moments include understanding why detailed JSON is returned, what happens on failure, and why HealthCheckService is injected. The quiz tests understanding of these steps and results.