NestJS Communication & Stakeholder Relations 3 — Questions and Answers
Question 1: Which NestJS feature allows you to intercept and transform outgoing responses before they reach the client?
- Guards
- Interceptors (Correct answer)
- Pipes
- Filters
Correct answer: Interceptors
Interceptors wrap method execution and can transform the response stream using RxJS operators.
Question 2: A product manager requests that all API errors follow a consistent JSON structure. What NestJS mechanism enforces this globally?
- Global pipe
- Global exception filter (Correct answer)
- Global guard
- Global interceptor
Correct answer: Global exception filter
A global exception filter catches all unhandled exceptions and serializes them into a uniform error response.
Question 3: In NestJS event-driven communication, what does @EventPattern() differ from @MessagePattern() in?
- @EventPattern() expects a reply; @MessagePattern() does not
- @EventPattern() handles fire-and-forget events; @MessagePattern() handles request-response (Correct answer)
- @EventPattern() is for HTTP; @MessagePattern() is for TCP
- @EventPattern() validates payloads; @MessagePattern() does not
Correct answer: @EventPattern() handles fire-and-forget events; @MessagePattern() handles request-response
@EventPattern() handles one-way event emission while @MessagePattern() follows the request-response pattern and returns data.
Question 4: Which class from @nestjs/swagger should you use to document the expected request body shape?
- @ApiParam()
- @ApiBody() (Correct answer)
- @ApiQuery()
- @ApiHeader()
Correct answer: @ApiBody()
@ApiBody() annotates the controller method to describe the expected body schema in Swagger.
Question 5: A frontend team complains about CORS errors when calling your NestJS API. What is the simplest fix?
- Add Authorization header manually
- Call app.enableCors() in main.ts (Correct answer)
- Install a third-party proxy
- Disable Helmet middleware
Correct answer: Call app.enableCors() in main.ts
app.enableCors() enables Cross-Origin Resource Sharing in NestJS with sensible defaults or configurable options.
Question 6: Which NestJS module facilitates communication between services using a Redis pub/sub transport?
- @nestjs/bull
- @nestjs/microservices with Redis transport (Correct answer)
- @nestjs/cache-manager
- @nestjs/schedule
Correct answer: @nestjs/microservices with Redis transport
@nestjs/microservices supports Redis as a transport, enabling pub/sub messaging between microservices.
Question 7: A stakeholder needs versioned APIs so old clients are not broken by new changes. Which NestJS feature supports this?
- Global prefixes
- API versioning via enableVersioning() (Correct answer)
- Route guards
- Dynamic modules
Correct answer: API versioning via enableVersioning()
NestJS supports URI, header, and media-type versioning through the enableVersioning() method in main.ts.
Which NestJS feature allows you to intercept and transform outgoing responses before they reach the client?