Recall & Review
beginner
What is a service in NestJS?
A service in NestJS is a class that handles business logic and data management. It is used to keep controllers simple and focused on handling requests.Click to reveal answer
beginner
How do you create a service using the NestJS CLI?
You run the command <code>nest generate service service-name</code> or <code>nest g s service-name</code>. This creates a service file with a class decorated by <code>@Injectable()</code>.Click to reveal answer
beginner
What does the
@Injectable() decorator do in a NestJS service?The <code>@Injectable()</code> decorator marks the class as a provider that can be injected into other classes, enabling dependency injection.Click to reveal answer
intermediate
Why should business logic be placed in services instead of controllers?
Placing business logic in services keeps controllers clean and focused on handling HTTP requests and responses. It also makes the logic reusable and easier to test.
Click to reveal answer
beginner
How do you inject a service into a controller in NestJS?
You inject a service by adding it as a parameter in the controller's constructor. NestJS automatically provides the instance if the service is registered in the module.
Click to reveal answer
Which decorator is used to mark a class as a service provider in NestJS?
✗ Incorrect
The @Injectable() decorator marks a class as a provider that can be injected into other classes.
What command creates a new service using the NestJS CLI?
✗ Incorrect
The command 'nest generate service service-name' creates a new service file with the proper structure.
Where should you put business logic in a NestJS application?
✗ Incorrect
Business logic belongs in services to keep controllers simple and reusable.
How do you make a service available to a controller?
✗ Incorrect
Services are injected into controllers through the constructor for dependency injection.
What is the main benefit of using services in NestJS?
✗ Incorrect
Services separate business logic from controllers, improving code organization and testability.
Explain how to create and use a service in NestJS from scratch.
Think about the steps from creating the service file to using it in a controller.
You got /4 concepts.
Why is dependency injection important when working with services in NestJS?
Consider how services are provided to other parts of the app.
You got /4 concepts.