0
0
NestJSframework~20 mins

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

Choose your learning style9 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.