NestJS Basic 5 — Questions and Answers
Question 1: What is Middleware in NestJS and when does it execute?
- A decorator that runs after the response is sent
- A function that runs before the route handler, with access to req and res (Correct answer)
- A service that transforms database results
- A pipe that validates route parameters
Correct answer: A function that runs before the route handler, with access to req and res
Middleware functions execute before the route handler and can modify the request/response or call next() to pass control forward.
Question 2: Which NestJS feature allows you to execute code before and after the entire application lifecycle?
- Interceptors
- Middleware
- Lifecycle hooks like OnModuleInit and OnApplicationBootstrap (Correct answer)
- Guards
Correct answer: Lifecycle hooks like OnModuleInit and OnApplicationBootstrap
NestJS lifecycle hooks (OnModuleInit, OnApplicationBootstrap, OnModuleDestroy, etc.) let you run logic at specific points in the app lifecycle.
Question 3: What is the @Res() decorator used for in NestJS?
- To inject a service response
- To access the underlying framework's native response object (Correct answer)
- To resolve a promise response
- To set response headers only
Correct answer: To access the underlying framework's native response object
@Res() injects the raw Express (or Fastify) response object, giving you full control over the response but bypassing NestJS response handling.
Question 4: In NestJS, what is the purpose of the ConfigModule?
- To configure routing rules
- To load and manage environment variables and configuration across the application (Correct answer)
- To set up database migrations
- To configure Swagger documentation
Correct answer: To load and manage environment variables and configuration across the application
ConfigModule (from @nestjs/config) loads .env files and provides a ConfigService to access environment variables throughout the app.
Question 5: When using class-validator with NestJS, which pipe must be globally enabled to trigger validation?
- ParseIntPipe
- ValidationPipe (Correct answer)
- ClassSerializerPipe
- TransformPipe
Correct answer: ValidationPipe
ValidationPipe must be applied (globally or per-route) to automatically validate incoming data against class-validator decorated DTOs.
Question 6: What is a DTO (Data Transfer Object) in the context of NestJS?
- A database model that maps to a table
- A plain object used to define the shape of data sent over the network (Correct answer)
- A decorator that transforms responses
- A service that transfers data between modules
Correct answer: A plain object used to define the shape of data sent over the network
A DTO is a TypeScript class defining the expected structure of incoming or outgoing data, often decorated with class-validator rules.
Question 7: How do you make a NestJS module available globally without importing it in every other module?
- Add @Global() decorator to the module class (Correct answer)
- List it in the AppModule's exports array only
- Use @Universal() on the module
- Register it with app.setGlobalModule() in main.ts
Correct answer: Add @Global() decorator to the module class
@Global() on a module makes its exported providers available application-wide without needing to import the module everywhere.
What is Middleware in NestJS and when does it execute?