0
0
NestJSframework~5 mins

Service creation in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A@Injectable()
B@Controller()
C@Service()
D@Module()
What command creates a new service using the NestJS CLI?
Anest new service service-name
Bnest create service service-name
Cnest generate service service-name
Dnest build service service-name
Where should you put business logic in a NestJS application?
AIn controllers
BIn services
CIn modules
DIn main.ts
How do you make a service available to a controller?
AInject it via the controller's constructor
BCall it directly without injection
CImport it in the controller file
DDeclare it in main.ts
What is the main benefit of using services in NestJS?
AAutomatically creates routes
BMakes the app slower
CRemoves the need for controllers
DSeparates business logic from request handling
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.