What is the purpose of dependency injection in NestJS?
Explanation:
Dependency injection is a design pattern where dependencies are 'injected' into a class rather than being directly instantiated within the class. This promotes modularity, testability, and reusability.
How do you inject a provider from a different module in NestJS?
Explanation:
To use a provider from a different module, you need to import the module that defines the provider into the module where you want to use it. This makes the provider available for injection within the importing module.
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 primary function of a module in NestJS?
Explanation:
Modules in NestJS act as containers for other components like controllers, providers, and services. They help structure the application, making it more maintainable and promoting code reusability.
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.
How can you access the currently logged-in user within your middleware in NestJS?
Explanation:
If you're using an authentication middleware in NestJS, it typically attaches the authenticated user object to the request object. You can access this user information within your middleware by referring to req.user
.
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.
What is the primary purpose of exception handling in a NestJS backend application?
Explanation:
Exception handling is crucial for preventing application crashes and providing meaningful error responses to clients when unexpected situations arise during request handling.
Which of the following is NOT a valid HTTP method for defining routes in NestJS?
Explanation:
NestJS uses standard HTTP methods for routing. RENDER
is not a standard HTTP method; it's often associated with server-side templating engines.
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 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.
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.
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.
After installing the NestJS CLI, what command is used to initiate a new NestJS project?
Explanation:
The nest new
command followed by your desired project name is the standard way to start a new NestJS project using the CLI.
What decorator is used for handling HTTP requests meant for updating resources?
Explanation:
The @Put()
decorator marks a controller method for handling HTTP PUT requests, typically used for complete updates of existing resources.