0
0
NestJSframework~10 mins

Health checks (Terminus) in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the TerminusModule in a NestJS module.

NestJS
import { Module } from '@nestjs/common';
import { [1] } from '@nestjs/terminus';

@Module({
  imports: [[1]],
})
export class AppModule {}
Drag options to blanks, or click blank then click option'
AHealthCheckModule
BHealthModule
CTerminusModule
DHealthTerminus
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect module names like HealthModule or HealthCheckModule.
Forgetting to import the TerminusModule.
2fill in blank
medium

Complete the code to create a health check endpoint using Terminus in a controller.

NestJS
import { Controller, Get } from '@nestjs/common';
import { HealthCheck, HealthCheckService } from '@nestjs/terminus';

@Controller('health')
export class HealthController {
  constructor(private health: HealthCheckService) {}

  @Get()
  @HealthCheck()
  check() {
    return this.health.[1]([]);
  }
}
Drag options to blanks, or click blank then click option'
Aexecute
Brun
Cperform
Dcheck
Attempts:
3 left
💡 Hint
Common Mistakes
Using methods like run() or execute() which do not exist on HealthCheckService.
Forgetting to call the method as a function.
3fill in blank
hard

Fix the error in the health indicator registration by completing the code.

NestJS
import { Injectable } from '@nestjs/common';
import { HealthIndicator, HealthIndicatorResult, HealthCheckError } from '@nestjs/terminus';

@Injectable()
export class DiskHealthIndicator extends HealthIndicator {
  async isHealthy(key: string): Promise<HealthIndicatorResult> {
    const diskFree = 100; // pretend value
    const isHealthy = diskFree > 50;
    if (!isHealthy) {
      throw new HealthCheckError('Disk space too low', [1]);
    }
    return this.getStatus(key, isHealthy, { diskFree });
  }
}
Drag options to blanks, or click blank then click option'
Athis.getStatus(key, isHealthy, { diskFree })
Bthis.getStatus(key, false, { diskFree })
Cthis.getStatus(key, true, { diskFree })
Dthis.getStatus(key, isHealthy)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing incomplete or incorrect status objects to HealthCheckError.
Omitting the third argument with details.
4fill in blank
hard

Fill both blanks to add a memory health indicator to the health check controller.

NestJS
import { Controller, Get } from '@nestjs/common';
import { HealthCheck, HealthCheckService, [1] } from '@nestjs/terminus';

@Controller('health')
export class HealthController {
  constructor(
    private health: HealthCheckService,
    private [2]: MemoryHealthIndicator
  ) {}

  @Get('memory')
  @HealthCheck()
  checkMemory() {
    return this.health.check([
      () => this.[2].checkHeap('memory_heap'),
    ]);
  }
}
Drag options to blanks, or click blank then click option'
AMemoryHealthIndicator
BMemoryIndicator
CMemoryCheck
DMemoryHealth
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like MemoryIndicator or MemoryCheck.
Mismatching the injected variable name and the class.
5fill in blank
hard

Fill all three blanks to create a custom health indicator and register it in the health check controller.

NestJS
import { Injectable } from '@nestjs/common';
import { HealthIndicator, HealthIndicatorResult, HealthCheckError } from '@nestjs/terminus';

@Injectable()
export class [1] extends HealthIndicator {
  async isHealthy(key: string): Promise<HealthIndicatorResult> {
    const serviceUp = true; // pretend check
    const isHealthy = serviceUp;
    if (!isHealthy) {
      throw new HealthCheckError('Service down', this.getStatus(key, isHealthy));
    }
    return this.getStatus(key, isHealthy);
  }
}

import { Controller, Get } from '@nestjs/common';
import { HealthCheck, HealthCheckService } from '@nestjs/terminus';

@Controller('health')
export class HealthController {
  constructor(
    private health: HealthCheckService,
    private [2]: [1]
  ) {}

  @Get('custom')
  @HealthCheck()
  checkCustom() {
    return this.health.check([
      () => this.[2].isHealthy('custom_service'),
    ]);
  }
}
Drag options to blanks, or click blank then click option'
ACustomHealthIndicator
BcustomHealthIndicator
CCustomIndicator
DcustomIndicator
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent names between class and injected variable.
Calling wrong methods or using wrong variable names.