Bird
Raised Fist0
NestJSframework~20 mins

Health checks (Terminus) in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Terminus Health Check Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this NestJS Terminus health check controller?
Consider this health check controller using Terminus in NestJS. What will the HTTP response body be when the /health endpoint is called?
NestJS
import { Controller, Get } from '@nestjs/common';
import { HealthCheck, HealthCheckService, HttpHealthIndicator } from '@nestjs/terminus';

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

  @Get()
  @HealthCheck()
  check() {
    return this.health.check([
      () => this.http.pingCheck('nestjs-docs', 'https://docs.nestjs.com'),
    ]);
  }
}
A{"status":"error","info":{"nestjs-docs":{"status":"up"}},"error":{},"details":{"nestjs-docs":{"status":"up"}}}
B{"status":"error","info":{},"error":{"nestjs-docs":{"status":"down"}},"details":{"nestjs-docs":{"status":"down"}}}
C{"status":"ok","info":{"nestjs-docs":{"status":"down"}},"error":{},"details":{"nestjs-docs":{"status":"down"}}}
D{"status":"ok","info":{"nestjs-docs":{"status":"up"}},"error":{},"details":{"nestjs-docs":{"status":"up"}}}
Attempts:
2 left
💡 Hint
The pingCheck returns status 'up' if the URL is reachable.
📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in this NestJS Terminus health check setup?
Identify which code snippet will cause a syntax error when defining a health check method in a NestJS controller using Terminus.
A
@HealthCheck()
check() {
  return this.health.check([
    () => this.http.pingCheck('google', 'https://google.com'),
  ]);
}
B
}
;)]  
,)'moc.elgoog//:sptth' ,'elgoog'(kcehCgnip.ptth.siht >= )(    
[(kcehc.htlaeh.siht nruter  
{ )(kcehc
)(kcehChtlaeH@
C
@HealthCheck
check() {
  return this.health.check([
    () => this.http.pingCheck('google', 'https://google.com'),
  ]);
}
D
@HealthCheck()
check() {
  return this.health.check([
    () => this.http.pingCheck('google', 'https://google.com')
  ]);
}
Attempts:
2 left
💡 Hint
Check if the decorator is properly called as a function.
❓ state_output
advanced
2:00remaining
What is the value of the 'status' field when a database health check fails?
Given this health check method in NestJS Terminus, what will be the 'status' field in the JSON response if the database ping fails?
NestJS
import { Controller, Get } from '@nestjs/common';
import { HealthCheck, HealthCheckService, TypeOrmHealthIndicator } from '@nestjs/terminus';

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

  @Get()
  @HealthCheck()
  check() {
    return this.health.check([
      () => this.db.pingCheck('database'),
    ]);
  }
}
A"error"
B"ok"
C"down"
D"unknown"
Attempts:
2 left
💡 Hint
The overall status is 'error' if any check fails.
🔧 Debug
advanced
2:00remaining
Why does this Terminus health check always return status 'error'?
This health check always returns status 'error' even though the URL is correct. What is the likely cause?
NestJS
import { Controller, Get } from '@nestjs/common';
import { HealthCheck, HealthCheckService, HttpHealthIndicator } from '@nestjs/terminus';

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

  @Get()
  @HealthCheck()
  check() {
    return this.health.check([
      () => this.http.pingCheck('nestjs-docs', 'https://docs.nestjs.com'),
      () => this.http.pingCheck('invalid-url', 'htp://invalid-url'),
    ]);
  }
}
AThe second pingCheck URL has a typo in the protocol causing failure.
BThe controller route 'health' is not registered.
CThe @HealthCheck decorator is missing parentheses.
DThe HealthCheckService is not injected properly.
Attempts:
2 left
💡 Hint
Check the URLs passed to pingCheck carefully.
🧠 Conceptual
expert
3:00remaining
Which option best describes how Terminus aggregates multiple health indicators?
In NestJS Terminus, when multiple health indicators are passed to HealthCheckService.check(), how is the overall health status determined?
AThe overall status is always 'ok' regardless of individual indicator results.
BIf all indicators return 'up', overall status is 'ok'; if any return 'down', overall status is 'error'.
CThe overall status is 'error' only if all indicators return 'down'.
DThe overall status is the status of the first indicator only.
Attempts:
2 left
💡 Hint
Think about how a system health check should behave if one part fails.

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

  1. Step 1: Understand Terminus role in NestJS

    Terminus is a tool designed to add health checks to monitor app status.
  2. 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.
  3. Final Answer:

    To add health checks that monitor if the app and its parts are working well -> Option B
  4. 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

  1. Step 1: Recall NestJS dependency injection syntax

    Use constructor with private readonly and type for proper injection.
  2. 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.
  3. Final Answer:

    constructor(private readonly healthCheckService: HealthCheckService) {} -> Option C
  4. 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?
async check() {
  return this.healthCheckService.check([
    () => this.dbHealthIndicator.pingCheck('database')
  ]);
}
medium
A. 200 OK with status 'ok' and details about database
B. 500 Internal Server Error with failure details
C. 404 Not Found because no route is defined
D. 302 Redirect to another URL

Solution

  1. Step 1: Understand healthCheckService.check behavior

    It returns 200 OK with status 'ok' if all checks pass.
  2. Step 2: Analyze the database pingCheck result

    If database is healthy, pingCheck passes, so overall status is 'ok'.
  3. Final Answer:

    200 OK with status 'ok' and details about database -> Option A
  4. Quick Check:

    Healthy check = 200 OK [OK]
Hint: Healthy checks return 200 OK with status 'ok' [OK]
Common Mistakes:
  • Assuming failure status on healthy database
  • Confusing route existence with health check response
  • Expecting redirects from health checks
4. Identify the error in this NestJS Terminus health check controller code:
@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')
    ]);
  }
}
medium
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

  1. Step 1: Check usage of httpHealthIndicator

    httpHealthIndicator is used but not injected or declared anywhere.
  2. Step 2: Verify constructor parameters

    Only healthCheckService is injected; httpHealthIndicator is missing.
  3. Final Answer:

    Missing injection of HttpHealthIndicator in constructor -> Option A
  4. 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

  1. Step 1: Understand combining multiple checks

    healthCheckService.check accepts an array of async functions for multiple checks.
  2. Step 2: Verify correct usage of indicators

    Use dbHealthIndicator.pingCheck and httpHealthIndicator.pingCheck as separate functions in the array.
  3. Step 3: Evaluate other options

    Separate controllers or nesting checks is incorrect; ignoring API check misses requirement.
  4. Final Answer:

    Use healthCheckService.check with an array of functions calling dbHealthIndicator.pingCheck and httpHealthIndicator.pingCheck -> Option D
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard