What is the difference between a provider and a service in NestJS?
Explanation:
While all services are providers in NestJS, not all providers are services. Services are specifically meant to encapsulate business logic, making them a specialized form of a provider.
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 the purpose of Providers in NestJS?
Explanation:
Providers are a key concept in NestJS for building reusable and modular code. They represent services, repositories, or any other logic that can be shared across your application.
Which decorator is used to apply middleware to a specific route in NestJS?
Explanation:
The @UseMiddleware()
decorator is used to bind middleware to specific routes or controllers. You can apply it at the method level for individual routes or at the controller level to affect all routes within that controller.
What is the recommended way to access query parameters in a NestJS controller?
Explanation:
The @Query()
decorator provides a clean and type-safe way to access query parameters from the request object. It allows retrieving specific parameters or the entire query object.
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.
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
.
How would you define a route parameter named 'id' in a NestJS route path?
Explanation:
In NestJS, route parameters are defined using the colon (:
) followed by the parameter name. This pattern captures the value from the URL and makes it accessible within the route handler.
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.
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.