NestJS Technology & Digital Applications 4 — Questions and Answers
Question 1: What is the purpose of `@EventPattern()` in a NestJS microservice?
- It replaces @MessagePattern() for HTTP events
- It handles fire-and-forget events where no response is expected (Correct answer)
- It subscribes to server-sent events on the client
- It emits events from the server to connected clients
Correct answer: It handles fire-and-forget events where no response is expected
@EventPattern() handles one-way event-based communication in microservices where the publisher doesn't await a response.
Question 2: Which NestJS module scope makes a provider instantiated per request?
- Scope.SINGLETON
- Scope.TRANSIENT
- Scope.REQUEST (Correct answer)
- Scope.PROTOTYPE
Correct answer: Scope.REQUEST
Scope.REQUEST creates a new provider instance for each incoming request, which is useful for per-request state like user context.
Question 3: How do you make a NestJS controller method return a streaming response?
- Return an Observable from the method and NestJS pipes it to the response (Correct answer)
- Use @Stream() decorator on the method
- Set Content-Type to text/stream manually
- Call res.stream() from the response object
Correct answer: Return an Observable from the method and NestJS pipes it to the response
Returning an RxJS Observable from a handler allows NestJS to subscribe and stream emitted values as a chunked HTTP response.
Question 4: What does enabling `strict mode` in NestJS's ValidationPipe do?
- Throws on any unrecognized property in the request body (Correct answer)
- Enables strict TypeScript checking
- Requires all DTOs to extend BaseDTO
- Validates query params as well as body
Correct answer: Throws on any unrecognized property in the request body
Setting forbidNonWhitelisted: true (strict mode) causes ValidationPipe to throw a 400 error if the request body contains properties not defined in the DTO.
Question 5: In NestJS, what is the `ExecutionContext` used for in guards and interceptors?
- Storing the current user session
- Accessing the underlying request/response objects and handler metadata (Correct answer)
- Defining execution order of middleware
- Caching resolved module references
Correct answer: Accessing the underlying request/response objects and handler metadata
ExecutionContext extends ArgumentsHost and provides methods to get the request/response objects and reflect metadata set on route handlers.
Question 6: Which NestJS CLI command generates a complete CRUD resource with module, controller, service, and DTO files?
- nest g module
- nest g controller
- nest g resource (Correct answer)
- nest g scaffold
Correct answer: nest g resource
`nest g resource` generates a full CRUD resource including module, controller, service, DTO, and entity files in one command.
Question 7: What is the purpose of the `@Cron()` decorator from @nestjs/schedule?
- Schedules a method to run on a time-based cron expression (Correct answer)
- Throttles API calls using a cron window
- Retries failed HTTP requests on a schedule
- Registers a recurring database migration
Correct answer: Schedules a method to run on a time-based cron expression
@Cron() schedules a method to execute automatically according to a cron expression (e.g., '0 * * * *' for every hour).
What is the purpose of `@EventPattern()` in a NestJS microservice?