NestJS Communication & Stakeholder Relations 5 — Questions and Answers
Question 1: Which NestJS mechanism allows a microservice to return data back to the caller in a request-response pattern?
- Emitting a reply event manually
- Returning a value or Observable from a @MessagePattern() handler (Correct answer)
- Using @EventPattern() with a callback
- Calling this.server.emit()
Correct answer: Returning a value or Observable from a @MessagePattern() handler
Returning a value or Observable from a @MessagePattern() handler automatically sends the reply to the requesting service.
Question 2: A DevOps stakeholder asks how to health-check the NestJS service. Which package provides a ready-made /health endpoint?
- @nestjs/terminus (Correct answer)
- @nestjs/swagger
- @nestjs/config
- @nestjs/schedule
Correct answer: @nestjs/terminus
@nestjs/terminus integrates with infrastructure health checkers and exposes a configurable /health endpoint.
Question 3: When a NestJS WebSocket client disconnects unexpectedly, which lifecycle hook is called on the gateway?
- @OnGatewayInit
- @OnGatewayDisconnect (Correct answer)
- @SubscribeMessage('disconnect')
- @OnGatewayDestroy
Correct answer: @OnGatewayDisconnect
Implementing OnGatewayDisconnect and its handleDisconnect() method lets the gateway react when a client drops.
Question 4: A project manager requires all inter-service messages to carry a correlation ID for tracing. Where is the best place to inject this in NestJS?
- Inside each service method manually
- In a global interceptor that wraps every outgoing request (Correct answer)
- In the database entity
- In the Swagger decorator
Correct answer: In a global interceptor that wraps every outgoing request
A global interceptor can attach a correlation ID to every outgoing context before the handler executes.
Question 5: Which property of the @WebSocketGateway() decorator limits connections to a specific namespace in Socket.IO?
- path
- namespace (Correct answer)
- room
- channel
Correct answer: namespace
The namespace property in @WebSocketGateway({ namespace: '/chat' }) scopes the gateway to a Socket.IO namespace.
Question 6: To communicate breaking API changes to consuming teams, which versioning strategy in NestJS embeds the version in the URL path?
- Header versioning
- Media type versioning
- URI versioning (Correct answer)
- Query string versioning
Correct answer: URI versioning
URI versioning prefixes routes with /v1/, /v2/, etc., making the version explicit and easy for teams to consume.
Question 7: A stakeholder notices that API error messages expose internal stack traces in production. What NestJS configuration prevents this?
- Disabling all exception filters
- Setting app.useGlobalFilters() with a filter that omits stack in production (Correct answer)
- Removing all try/catch blocks
- Using HTTP-only cookies
Correct answer: Setting app.useGlobalFilters() with a filter that omits stack in production
A custom global exception filter can inspect NODE_ENV and strip stack trace details from the response in production.
Which NestJS mechanism allows a microservice to return data back to the caller in a request-response pattern?