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()
✗ 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?
Anest new service service-name
Bnest create service service-name
Cnest generate service service-name
Dnest build service service-name
✗ 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?
AIn controllers
BIn services
CIn modules
DIn main.ts
✗ Incorrect
Business logic belongs in services to keep controllers simple and reusable.
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
✗ Incorrect
Services are injected into controllers through the constructor for dependency injection.
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
✗ 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.
Practice
(1/5)
1.
What is the main purpose of creating a service in a NestJS application?
easy
A. To handle HTTP requests directly
B. To style the user interface
C. To define database schemas
D. To hold the business logic separate from controllers
Solution
Step 1: Understand the role of services in NestJS
Services are designed to contain business logic, keeping controllers clean and focused on handling requests.
Step 2: Differentiate services from controllers and other parts
Controllers handle HTTP requests, schemas define data structure, and styling is unrelated to services.
Final Answer:
To hold the business logic separate from controllers -> Option D
Quick Check:
Service purpose = business logic separation [OK]
Hint: Services = business logic holders, not request handlers [OK]
Common Mistakes:
Confusing services with controllers
Thinking services handle HTTP requests
Assuming services define database schemas
2.
Which decorator is used to make a class injectable as a service in NestJS?
easy
A. @Injectable()
B. @Module()
C. @Controller()
D. @Service()
Solution
Step 1: Identify the decorator for service injection
In NestJS, @Injectable() marks a class as a provider that can be injected.
Step 2: Differentiate from other decorators
@Controller() is for controllers, @Module() for modules, and @Service() does not exist in NestJS.
Final Answer:
@Injectable() -> Option A
Quick Check:
Service decorator = @Injectable() [OK]
Hint: Remember: Injectable means ready to inject [OK]
Common Mistakes:
Using @Controller() instead of @Injectable()
Confusing @Module() with service decorator
Assuming @Service() exists in NestJS
3.
Given the following service code, what will be the output when getHello() is called?
import { Injectable } from '@nestjs/common';
@Injectable()
export class HelloService {
getHello(): string {
return 'Hello, NestJS!';
}
}
medium
A. "Hello, NestJS!"
B. undefined
C. Error: Method not found
D. "Hello World!"
Solution
Step 1: Analyze the getHello method
The method returns the string 'Hello, NestJS!'.
Step 2: Determine the output of calling getHello()
Calling getHello() returns exactly the string defined, no errors or undefined.
Final Answer:
"Hello, NestJS!" -> Option A
Quick Check:
Method return = "Hello, NestJS!" [OK]
Hint: Read method return value carefully [OK]
Common Mistakes:
Assuming method returns undefined
Confusing output with error messages
Mixing up string content
4.
Identify the error in this service code snippet:
import { Injectable } from '@nestjs/common';
@Injectable()
export class MathService {
add(a: number, b: number) {
return a + b
}
}
medium
A. Missing parentheses in method declaration
B. Missing return type annotation for add method
C. No error, code is correct
D. Missing semicolon after return statement
Solution
Step 1: Check syntax correctness
The method add correctly takes two numbers and returns their sum. Semicolons are optional in TypeScript.
Step 2: Verify method declaration and return type
Return type annotation is optional; TypeScript infers it. Parentheses are present and correct.
You want to create a service that provides a list of users and inject it into a controller. Which of the following is the correct way to create and inject the service?
1. Create service with @Injectable() 2. Define a method getUsers() returning an array 3. Inject service in controller constructor 4. Use service method inside controller method
hard
A. Create service without @Injectable(), inject with @Inject() decorator
B. Create service with @Injectable(), inject via constructor parameter
C. Create service with @Controller(), inject via property decorator
D. Create service with @Module(), inject manually inside methods
Solution
Step 1: Create service with @Injectable()
This decorator makes the service injectable and reusable.
Step 2: Inject service via constructor parameter in controller
Constructor injection is the NestJS standard for dependency injection.
Step 3: Use service methods inside controller methods
This keeps code modular and clean.
Final Answer:
Create service with @Injectable(), inject via constructor parameter -> Option B
Quick Check:
Service injection = @Injectable() + constructor [OK]
Hint: Use @Injectable() and constructor injection [OK]
Common Mistakes:
Omitting @Injectable() on service
Using @Controller() on service
Injecting service incorrectly with property decorators
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.