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.
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.
Which decorator is used to define a controller in NestJS?
Explanation:
The @Controller()
decorator is essential for marking a class as a controller. This enables NestJS to recognize it as a handler for incoming HTTP requests.
Which decorator is used to mark a class as a provider in NestJS?
Explanation:
The @Injectable()
decorator is crucial for making a class available for dependency injection. This informs NestJS that the class can be provided as a dependency to other components.
What is a potential drawback of using singleton providers excessively in a large application?
Explanation:
While singletons offer benefits like shared state and reduced instantiation overhead, overusing them can lead to increased memory usage (as instances persist), slower startup (due to potential initialization chains), and challenges in isolating components during testing.
Which programming language forms the foundation of NestJS?
Explanation:
NestJS is built on top of Node.js, and it primarily uses either JavaScript or TypeScript (a superset of JavaScript) for building applications.
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.
How does Dependency Injection work in NestJS?
Explanation:
Dependency Injection in NestJS is handled by its internal injector. When a component (like a controller) has a dependency specified in its constructor, the injector automatically resolves and provides an instance of that dependency.
When would you choose to create a custom exception filter instead of using a built-in one?
Explanation:
Custom filters provide the flexibility to tailor error responses, logging, or any specific actions needed for particular exception scenarios in your application.
What is the order of execution for middleware applied at the controller and route level in NestJS?
Explanation:
In NestJS, middleware follows a specific execution order. When a request arrives, any middleware applied at the controller level is executed first. Afterward, the middleware specific to the requested route is executed.
When using an Interceptor to modify the response, what is the best way to ensure type safety?
Explanation:
For type safety, define generics with the NestInterceptor
interface, specifying the expected request and response types, allowing TypeScript to perform type checking.
What is the purpose of the @Get()
decorator in NestJS?
Explanation:
The @Get()
decorator is used to mark a controller method as a handler for incoming HTTP GET requests. It specifies that the method should be invoked when a GET request is made to the associated route.
What is the advantage of using built-in HTTP exception filters in NestJS?
Explanation:
NestJS's built-in filters simplify error handling by providing ready-to-use responses for standard HTTP errors, reducing the need for repetitive code.
What is the role of the next()
function in NestJS middleware?
Explanation:
The next()
function is crucial for controlling the flow within middleware. Calling next()
delegates the responsibility to the subsequent middleware in line or, if it's the last middleware, to the route handler.
Which decorator is used to define an Interceptor class in NestJS?
Explanation:
The @Interceptor()
decorator from @nestjs/common
is specifically designed to mark a class as an Interceptor in NestJS.