NestJS Technology & Digital Applications 2 — Questions and Answers
Question 1: Which NestJS decorator is used to define a class as a WebSocket gateway?
- @WebSocket()
- @Gateway()
- @WebSocketGateway() (Correct answer)
- @SocketServer()
Correct answer: @WebSocketGateway()
@WebSocketGateway() marks a class as a WebSocket gateway, enabling real-time bidirectional communication.
Question 2: In NestJS, what is the purpose of the ConfigModule?
- To configure database connections only
- To load and expose environment variables application-wide (Correct answer)
- To configure HTTP request parsing
- To register middleware globally
Correct answer: To load and expose environment variables application-wide
ConfigModule loads environment variables (via .env files or process.env) and makes them accessible via ConfigService throughout the app.
Question 3: Which built-in NestJS pipe validates and transforms incoming request data against a DTO class?
- ValidationPipe (Correct answer)
- ParseIntPipe
- TransformPipe
- SanitizePipe
Correct answer: ValidationPipe
ValidationPipe uses class-validator decorators on DTOs to automatically validate and optionally transform incoming request payloads.
Question 4: What does the `@MessagePattern()` decorator do in a NestJS microservice?
- Defines an HTTP route handler
- Subscribes to a message broker topic
- Maps an incoming message pattern to a handler method (Correct answer)
- Emits events to other services
Correct answer: Maps an incoming message pattern to a handler method
@MessagePattern() tells the microservice which message pattern (string or object) should trigger a specific handler method.
Question 5: How does NestJS implement the concept of 'lazy loading' for modules?
- Using dynamic imports in the module factory
- Using LazyModule.register()
- Via the LazyModuleLoader service to load modules on-demand at runtime (Correct answer)
- By setting lazy: true in @Module() metadata
Correct answer: Via the LazyModuleLoader service to load modules on-demand at runtime
LazyModuleLoader allows modules to be loaded on-demand at runtime rather than at application startup, reducing initial load time.
Question 6: What is the role of a NestJS 'interceptor' in the request lifecycle?
- Block unauthorized requests before they reach controllers
- Bind extra logic before/after method execution and transform results (Correct answer)
- Parse and validate route parameters
- Register global exception handlers
Correct answer: Bind extra logic before/after method execution and transform results
Interceptors use the AOP pattern to bind extra logic before/after method execution, transform results, extend behavior, or override functions.
Question 7: Which NestJS transport is used when integrating with Redis for microservice communication?
- Transport.MQTT
- Transport.REDIS (Correct answer)
- Transport.NATS
- Transport.KAFKA
Correct answer: Transport.REDIS
Transport.REDIS uses Redis pub/sub to facilitate message passing between NestJS microservices.
Which NestJS decorator is used to define a class as a WebSocket gateway?