NestJs Questions
Question 1
Which decorator is used to define a basic GET route in NestJS?
Explanation:
The @Get()
decorator is specifically designed to handle HTTP GET requests. The other decorators are used for different HTTP methods or route configuration.
Question 2
How are controllers related to modules in NestJS?
Explanation:
Controllers are always defined within a specific module in NestJS. This association determines their scope and how they are managed within the application's structure.
Question 3
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.
Question 4
What is the role of Providers in NestJS?
Explanation:
Providers are fundamental to NestJS's dependency injection system. They can be classes annotated with @Injectable()
, and their instances can be injected into constructors of other components, promoting code reusability and testability.
Question 5
What is NOT a benefit of using Dependency Injection in NestJS?
Explanation:
Dependency Injection promotes loose coupling, not increased coupling. It allows components to depend on abstractions (interfaces) rather than concrete implementations.
Question 6
What is the primary programming language used for developing NestJS applications?
Explanation:
NestJS is built on top of Node.js, which uses JavaScript as its primary programming language. While TypeScript is often preferred, you can write NestJS applications directly in JavaScript.
Question 7
When using @Inject()
, what should you provide as the argument?
Explanation:
The @Inject()
decorator expects the injection token of the dependency as its argument. This token can be a class, a string, a symbol, or any other value that uniquely identifies the dependency.
Question 8
What is the purpose of the 'getStatus()' method in a custom exception filter?
Explanation:
The getStatus()
method allows the filter to access the appropriate HTTP status code from the caught exception, enabling it to set the correct status code for the error response.
Question 9
How can you define multiple route paths for a single controller method in NestJS?
Explanation:
NestJS allows defining multiple route paths for a single method by providing an array of strings as the first argument to the route decorator (@Get()
, @Post()
, etc.).
Question 10
Can a Controller access a Service's methods in NestJS?
Explanation:
In NestJS, a Controller can access a Service's methods by injecting an instance of the Service through the Controller's constructor. This is facilitated by NestJS's Dependency Injection mechanism.