🦅 NestJS Advanced

How do you implement health checks in NestJS?

Answer

The @nestjs/terminus module provides health check infrastructure for Kubernetes liveness and readiness probes and monitoring systems. Import TerminusModule and create a health controller: @Get('health') @HealthCheck() check() { return this.health.check([ () => this.db.pingCheck('database'), () => this.http.pingCheck('nestjs-docs', 'https://docs.nestjs.com'), () => this.memory.checkHeap('memory_heap', 150 * 1024 * 1024), ]); }. Built-in indicators include database (TypeORM, Mongoose), HTTP, Redis, microservice, memory, and disk. Returns a structured JSON report with status ('ok' or 'error') per indicator. Expose /health/live for liveness (is the process running?) and /health/ready for readiness (are all dependencies healthy?).