NestJS NestJS Architecture & Design Patterns 1 — Questions and Answers
Question 1: Which NestJS decorator is used to define a class as a module?
- @Injectable()
- @Module() (Correct answer)
- @Controller()
- @Component()
Correct answer: @Module()
The @Module() decorator marks a class as a NestJS module that organizes related providers, controllers, and imports.
Question 2: What is the purpose of the `providers` array in the @Module() decorator?
- To list imported modules
- To register services that can be injected within the module (Correct answer)
- To declare HTTP controllers
- To export public APIs
Correct answer: To register services that can be injected within the module
The `providers` array registers services (and other injectables) with the NestJS IoC container so they can be injected within that module.
Question 3: Which pattern does NestJS primarily use for organizing application logic?
- MVC
- MVVM
- Modular architecture with DI (Correct answer)
- Microkernel
Correct answer: Modular architecture with DI
NestJS uses a modular architecture combined with dependency injection (DI) to organize and decouple application components.
Question 4: What does the `exports` array in @Module() control?
- Which controllers are public
- Which providers can be used by other modules (Correct answer)
- The order of middleware execution
- Which guards are applied globally
Correct answer: Which providers can be used by other modules
The `exports` array specifies which providers from the current module are available for other modules that import it.
Question 5: Which NestJS concept allows a module to be shared across the entire application without re-importing?
- Dynamic Module
- Feature Module
- Global Module (Correct answer)
- Core Module
Correct answer: Global Module
A Global Module decorated with @Global() makes its exported providers available everywhere without requiring explicit imports.
Question 6: What is the role of the `AppModule` in a NestJS application?
- It handles HTTP requests directly
- It serves as the root module that bootstraps the entire application (Correct answer)
- It defines global guards
- It manages database connections
Correct answer: It serves as the root module that bootstraps the entire application
AppModule is the root module passed to NestFactory.create(), which NestJS uses as the starting point to build the dependency graph.
Which NestJS decorator is used to define a class as a module?