A feature module is a module that groups related components, services, and controllers to organize code by functionality. It helps keep the app modular and easier to maintain.
Click to reveal answer
beginner
How do feature modules improve a NestJS application?
They separate concerns by grouping related code, making the app easier to understand, test, and scale. They also enable lazy loading and better dependency management.
Click to reveal answer
beginner
Which decorator is used to define a feature module in NestJS?
The @Module() decorator is used to define a feature module. It takes metadata like imports, controllers, providers, and exports to configure the module.
Click to reveal answer
intermediate
Can feature modules import other modules in NestJS? Why?
Yes, feature modules can import other modules to use their exported providers. This allows sharing functionality and keeps code DRY (Don't Repeat Yourself).
Click to reveal answer
intermediate
What is the difference between a root module and a feature module in NestJS?
The root module is the main app module that bootstraps the application. Feature modules are additional modules that group related features and are imported into the root or other modules.
Click to reveal answer
Which decorator defines a feature module in NestJS?
A@Module()
B@Injectable()
C@Controller()
D@Component()
✗ Incorrect
The @Module() decorator is used to define modules, including feature modules, in NestJS.
What is the main purpose of feature modules in NestJS?
ABootstrap the application
BHandle HTTP requests
CGroup related code by functionality
DManage database connections
✗ Incorrect
Feature modules group related components, services, and controllers to organize code by functionality.
Can a feature module import another feature module in NestJS?
AOnly controllers can be imported
BNo, modules must be isolated
COnly root modules can import feature modules
DYes, to share providers and controllers
✗ Incorrect
Feature modules can import other modules to reuse exported providers and controllers.
Which of these is NOT part of the @Module() metadata in a feature module?
Aroutes
Bproviders
Cimports
Dcontrollers
✗ Incorrect
The @Module() decorator does not have a 'routes' property; routing is handled by controllers.
What is the root module in a NestJS app?
AA module that handles database connections
BThe main module that bootstraps the app
CA feature module for user management
DA module that only contains services
✗ Incorrect
The root module is the main app module that starts the application.
Explain what a feature module is in NestJS and why it is useful.
Think about how you organize tools in different boxes by their use.
You got /4 concepts.
Describe how feature modules interact with each other in a NestJS application.
Consider how teams share resources to work better together.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a feature module in NestJS?
easy
A. To manage HTTP requests without controllers
B. To group related controllers and services for better organization
C. To replace the main application module
D. To handle database connections directly
Solution
Step 1: Understand the role of feature modules
Feature modules group related parts of the app like controllers and services to keep code organized.
Step 2: Compare options with this role
Only To group related controllers and services for better organization describes grouping related controllers and services, which matches the purpose.
Final Answer:
To group related controllers and services for better organization -> Option B
Quick Check:
Feature modules organize code = D [OK]
Hint: Feature modules group related parts of your app [OK]
Confusing feature modules with the main app module
Assuming feature modules replace controllers
2. Which of the following is the correct way to define a feature module in NestJS?
easy
A. @Controller({}) export class FeatureModule {}
B. @Injectable() export class FeatureModule {}
C. @Module({ controllers: [], providers: [] }) export class FeatureModule {}
D. export class FeatureModule {}
Solution
Step 1: Recall the decorator for modules
Feature modules use the @Module decorator with metadata like controllers and providers.
Step 2: Match options with correct decorator
Only @Module({ controllers: [], providers: [] }) export class FeatureModule {} uses @Module with controllers and providers arrays, which is correct syntax.
Final Answer:
@Module({ controllers: [], providers: [] }) export class FeatureModule {} -> Option C
Quick Check:
@Module decorator defines modules = B [OK]
Hint: Use @Module decorator with controllers and providers [OK]
Common Mistakes:
Using @Controller or @Injectable instead of @Module
Forgetting to add the decorator
Defining empty classes without decorators
3. Given this feature module code, what will be the output when the getHello() method is called from HelloController?
@Module({
controllers: [HelloController],
providers: [HelloService],
})
export class HelloModule {}
@Injectable()
export class HelloService {
getHello() {
return 'Hello from service!';
}
}
@Controller()
export class HelloController {
constructor(private readonly helloService: HelloService) {}
@Get()
getHello() {
return this.helloService.getHello();
}
}
medium
A. undefined
B. "Hello from controller!"
C. Runtime error due to missing provider
D. "Hello from service!"
Solution
Step 1: Understand dependency injection in the module
The HelloService is provided in the module and injected into HelloController.
Step 2: Trace the method call
The controller's getHello calls helloService.getHello(), which returns 'Hello from service!'.
Final Answer:
"Hello from service!" -> Option D
Quick Check:
Service method returns string = C [OK]
Hint: Controller calls service method returning string [OK]
Common Mistakes:
Assuming controller returns its own string
Thinking provider is missing causing error
Confusing method names or return values
4. Identify the error in this feature module definition:
@Module({
controllers: [UserController],
providers: [UserService],
})
export class UserModule {}
@Injectable()
export class UserService {}
@Controller()
export class UserController {
constructor(userService: UserService) {}
}
medium
A. Missing access modifier 'private' in constructor injection
B. UserService is not decorated with @Injectable()
C. UserController is missing @Controller() decorator
D. UserService is not listed in providers array
Solution
Step 1: Check constructor injection syntax
In NestJS, injected dependencies must have an access modifier like private or public to create a class property.
Step 2: Identify the missing modifier
The constructor parameter 'userService' lacks 'private' or 'public', so it won't be assigned automatically.
Final Answer:
Missing access modifier 'private' in constructor injection -> Option A
Quick Check:
Constructor injection needs access modifier = A [OK]
Forgetting private/public in constructor parameters
Assuming @Injectable is missing when it is present
Confusing decorator placement
5. You want to create a feature module that imports another feature module to reuse its services. Which of the following is the correct way to import AuthModule into UserModule?
hard
A. @Module({ imports: [AuthModule], controllers: [UserController], providers: [UserService] }) export class UserModule {}
B. @Module({ controllers: [UserController], providers: [UserService] }) export class UserModule { constructor(private authModule: AuthModule) {} }
C. @Module({ exports: [AuthModule], controllers: [UserController], providers: [UserService] }) export class UserModule {}
D. @Module({ imports: [], controllers: [UserController], providers: [UserService] }) export class UserModule {}
Solution
Step 1: Understand module imports in NestJS
To reuse services from another module, you must import that module in the imports array.
Step 2: Identify correct import usage
@Module({ imports: [AuthModule], controllers: [UserController], providers: [UserService] }) export class UserModule {} correctly adds AuthModule to imports, allowing UserModule to use its exported providers.
Final Answer:
@Module({ imports: [AuthModule], controllers: [UserController], providers: [UserService] }) export class UserModule {} -> Option A
Quick Check:
Import modules via imports array = A [OK]
Hint: Use imports array to include other feature modules [OK]
Common Mistakes:
Trying to inject modules via constructor instead of imports
Using exports instead of imports to include modules
Leaving imports array empty when reusing modules
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.