NestJS NestJS 2 — Questions and Answers
Question 1: Which decorator is used to define a NestJS middleware class?
- @Middleware()
- @Injectable() (Correct answer)
- @UseMiddleware()
- @Module()
Correct answer: @Injectable()
NestJS middleware classes are decorated with @Injectable() and must implement the NestMiddleware interface.
Question 2: How do you apply middleware to specific routes in a NestJS module?
- Using @UseMiddleware() on the controller
- Configuring it in the configure() method of the module class (Correct answer)
- Passing it to the @Module() decorator's middleware array
- Registering it in the providers array of the module
Correct answer: Configuring it in the configure() method of the module class
NestJS modules implement the NestModule interface and use the configure() method with MiddlewareConsumer to apply middleware to routes.
Question 3: What is the purpose of a NestJS Guard?
- To transform request/response data
- To handle exceptions globally
- To determine whether a request should be handled by the route handler (Correct answer)
- To intercept and modify HTTP headers
Correct answer: To determine whether a request should be handled by the route handler
Guards implement the CanActivate interface and decide whether a given request will be handled by the route handler, typically used for authorization.
Question 4: Which interface must a NestJS Guard implement?
- GuardInterface
- CanActivate (Correct answer)
- Interceptor
- NestGuard
Correct answer: CanActivate
NestJS Guards must implement the CanActivate interface, which requires a canActivate() method returning a boolean or Observable<boolean>.
Question 5: What does the @Roles() decorator typically represent in NestJS?
- A built-in NestJS decorator for RBAC
- A custom metadata decorator used with Guards for role-based access control (Correct answer)
- A database query decorator
- A decorator for defining service roles
Correct answer: A custom metadata decorator used with Guards for role-based access control
@Roles() is a custom metadata decorator created using SetMetadata() that stores role data, which Guards then read via the Reflector service.
Question 6: Which NestJS class is used to read custom metadata set by decorators like @Roles()?
- MetadataReader
- Reflector (Correct answer)
- ExecutionContext
- ContextFactory
Correct answer: Reflector
The Reflector class is injected into Guards and Interceptors to read custom metadata attached to routes via decorators.
Question 7: In NestJS, what is the correct order of the request lifecycle?
- Guards → Interceptors → Pipes → Route Handler (Correct answer)
- Pipes → Guards → Interceptors → Route Handler
- Interceptors → Guards → Pipes → Route Handler
- Route Handler → Guards → Pipes → Interceptors
Correct answer: Guards → Interceptors → Pipes → Route Handler
NestJS processes requests in this order: Middleware → Guards → Interceptors (pre) → Pipes → Route Handler → Interceptors (post) → Exception Filters.
Which decorator is used to define a NestJS middleware class?