What is the purpose of the Root module (AppModule) in a NestJS application?
The Root module (AppModule) is the main module that NestJS uses to start the application. It organizes and connects all other modules, controllers, and providers.
Click to reveal answer
beginner
How do you define the Root module (AppModule) in NestJS?
You define the Root module by creating a class decorated with @Module() that lists imports, controllers, and providers. This class is usually named AppModule.
Click to reveal answer
beginner
What are the main properties inside the @Module() decorator in AppModule?
The main properties are: imports (other modules to include), controllers (handle incoming requests), and providers (services or helpers).
Click to reveal answer
intermediate
Why is the Root module important for dependency injection in NestJS?
The Root module registers providers that can be injected into controllers and other providers throughout the app, enabling easy sharing of services.
Click to reveal answer
intermediate
What happens if you forget to include a controller or provider in the AppModule?
NestJS will not recognize or use that controller or provider, so your app might not respond to requests or services might not work as expected.
Click to reveal answer
What decorator is used to define the Root module in NestJS?
A@Module()
B@Controller()
C@Injectable()
D@Root()
✗ Incorrect
The @Module() decorator is used to define modules, including the Root module (AppModule).
Which property inside @Module() lists the controllers for the module?
Aimports
Bproviders
Ccontrollers
Dexports
✗ Incorrect
The 'controllers' property lists all controllers that handle incoming requests in the module.
Where do you register services to be used across the app in the Root module?
Aproviders
Bcontrollers
Cimports
Dexports
✗ Incorrect
Services and helpers are registered in the 'providers' array to be injectable throughout the app.
What is the default name for the Root module class in a NestJS app?
AMainModule
BRootModule
CCoreModule
DAppModule
✗ Incorrect
By convention, the Root module class is named AppModule.
If a module is not imported in AppModule, what happens?
AIts controllers and providers are available globally.
BIts controllers and providers are not available in the app.
CThe app will crash immediately.
DNestJS imports it automatically.
✗ Incorrect
Modules must be imported in AppModule or other modules to be available; otherwise, their parts are not accessible.
Explain the role of the Root module (AppModule) in a NestJS application.
Think of it as the main organizer that connects all parts of your app.
You got /4 concepts.
Describe what happens if you forget to add a controller or provider to the AppModule.
Missing registration means NestJS doesn't know about that part.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the AppModule in a NestJS application?
easy
A. It serves as the root module that organizes controllers, providers, and imports.
B. It handles HTTP requests directly without controllers.
C. It is used only for database connections.
D. It runs background tasks without user interaction.
Solution
Step 1: Understand the role of AppModule
The AppModule is the root module that organizes the app's structure by grouping controllers, providers, and imports.
Step 2: Compare with other options
Options A, B, and D describe specific tasks that are handled by other parts of the app, not the root module itself.
Final Answer:
It serves as the root module that organizes controllers, providers, and imports. -> Option A
Quick Check:
Root module = organizes app parts [OK]
Hint: AppModule organizes app parts as root module [OK]
Common Mistakes:
Thinking AppModule handles requests directly
Confusing AppModule with service or controller
Assuming AppModule is only for database setup
2. Which of the following is the correct way to define the AppModule using the @Module decorator in NestJS?
easy
A. @Module({ controllers: [], providers: [] }) export class AppModule {}
B. export class AppModule {}
C. @Module({ controllers: [], providers: [] }) class AppModule {}
D. class AppModule {}
Solution
Step 1: Recognize correct @Module syntax
The @Module decorator requires an object with properties like controllers and providers, and must decorate an exported class.
Step 2: Check each option
@Module({ controllers: [], providers: [] }) export class AppModule {} correctly uses @Module with an object and exports the class. Options B and D lack the decorator, and C misses export keyword.
Final Answer:
@Module({ controllers: [], providers: [] }) export class AppModule {} -> Option A
Quick Check:
@Module + export class = correct syntax [OK]
Hint: Use @Module decorator with export class [OK]
Common Mistakes:
Forgetting to export the AppModule class
Missing the @Module decorator
Not passing an object to @Module
3. Given this AppModule code, what will be the output when the app starts?
import { Module } from '@nestjs/common';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';
@Module({
controllers: [CatsController],
providers: [CatsService],
})
export class AppModule {}
medium
A. The app starts but CatsService is not available.
B. The app throws an error because providers are missing.
C. The app starts but CatsController is not registered.
D. The app starts with CatsController and CatsService registered.
Solution
Step 1: Analyze the @Module configuration
The AppModule imports CatsController in controllers and CatsService in providers, so both are registered properly.
Step 2: Understand NestJS app startup
With both controller and provider registered, the app starts normally with these components available.
Final Answer:
The app starts with CatsController and CatsService registered. -> Option D
Quick Check:
Controllers and providers registered = app starts [OK]
Hint: Controllers and providers in @Module start app correctly [OK]
Common Mistakes:
Assuming missing imports cause errors here
Thinking providers are optional for controllers
Confusing registration with usage
4. Identify the error in this AppModule code snippet:
import { Module } from '@nestjs/common';
@Module({
controllers: CatsController,
providers: [CatsService],
})
export class AppModule {}
medium
A. AppModule class should not be exported.
B. Controllers should be an array, not a single value.
C. Missing import statement for CatsService.
D. Providers should be an object, not an array.
Solution
Step 1: Check the controllers property type
The controllers property must be an array, but here it is assigned a single value CatsController without brackets.
Step 2: Verify other parts
Providers are correctly an array. Import statements are not shown but assumed correct. Exporting AppModule is correct.
Final Answer:
Controllers should be an array, not a single value. -> Option B
Quick Check:
Controllers must be array [OK]
Hint: Controllers and providers must be arrays [OK]
Common Mistakes:
Using single controller without array brackets
Confusing providers array with object
Forgetting to export AppModule
5. You want to add a new feature module called UsersModule to your NestJS app. How should you modify the AppModule to include it properly?
hard
A. Add UsersModule to the controllers array in @Module decorator.
B. Add UsersModule to the providers array in @Module decorator.
C. Add UsersModule to the imports array in @Module decorator.
D. Create a new AppModule inside UsersModule.
Solution
Step 1: Understand module composition in NestJS
Feature modules like UsersModule are imported into the root module via the imports array to connect them.
Step 2: Evaluate options
Controllers and providers arrays are for specific controllers and services, not modules. Creating a new AppModule inside UsersModule is incorrect structure.
Final Answer:
Add UsersModule to the imports array in @Module decorator. -> Option C
Quick Check:
Feature modules go in imports [OK]
Hint: Add feature modules to imports array [OK]
Common Mistakes:
Adding modules to controllers or providers
Creating multiple AppModules
Forgetting to import feature modules
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.