NestJs Questions
Question 1
How do you access query parameters in a NestJS controller method?
Explanation:
The @Query()
decorator provides access to query parameters within a controller method. It allows you to retrieve values passed as key-value pairs in the URL's query string.
Question 2
What is the purpose of the return
statement in a NestJS controller method?
Explanation:
The value returned from a NestJS controller method forms the response sent back to the client. This can be a simple object, a string, or more complex data.
Question 3
Which decorator is used to define a custom exception filter in NestJS?
Explanation:
The @Catch()
decorator is specifically designed to mark a class as an exception filter, allowing it to handle specific exception types.
Question 4
What is the role of the 'HttpException' class in NestJS exception handling?
Explanation:
The HttpException
class provides a convenient way to create custom exceptions that carry appropriate HTTP status codes, making it easy to send meaningful error responses to clients.
Question 5
What is the primary function of middleware in NestJS?
Explanation:
Middleware in NestJS acts as a bridge between incoming requests and the route handlers. It allows you to perform tasks like logging, authentication, or data transformation before the request reaches the controller.
Question 6
Which decorator allows you to access route parameters in NestJS?
Explanation:
The @Param()
decorator is used to inject route parameters into controller methods. It allows you to access values from the URL path defined in your route.
Question 7
What is the purpose of the @Body()
decorator?
Explanation:
The @Body()
decorator is used to access the entire request body sent by the client. It allows you to retrieve data sent in formats like JSON or form data.
Question 8
What is the primary purpose of using Interceptors in NestJS?
Explanation:
Interceptors in NestJS provide a mechanism to intercept and potentially modify incoming requests before they reach route handlers or outgoing responses before they are sent back to the client.
Question 9
What is a common use case for middleware in NestJS?
Explanation:
One of the most common use cases for middleware in NestJS is to handle authentication and authorization. Middleware provides a centralized location to verify user credentials, check permissions, and control access to protected routes.
Question 10
What is the role of the 'main.ts' file in a NestJS project?
Explanation:
The main.ts
file is the entry point of your NestJS application. It bootstraps the NestJS application, often setting up the listening port and other core configurations.