NestJS Introduction 3 — Questions and Answers
Question 1: Which HTTP adapter does NestJS use by default under the hood?
- Koa
- Fastify
- Express (Correct answer)
- Hapi
Correct answer: Express
NestJS uses Express as its default HTTP platform, though it can be swapped for Fastify for higher performance.
Question 2: What does the 'exports' array in @Module() define?
- Routes exposed publicly
- Providers that other modules can import and use (Correct answer)
- Controllers visible to the app
- Guards applied globally
Correct answer: Providers that other modules can import and use
The 'exports' array specifies which providers from the current module should be available to other modules that import it.
Question 3: How do you switch NestJS from Express to Fastify?
- Set adapter: 'fastify' in nest-cli.json
- Pass FastifyAdapter to NestFactory.create() (Correct answer)
- Install @nestjs/fastify and restart
- Use @UseFastify() on AppModule
Correct answer: Pass FastifyAdapter to NestFactory.create()
You import FastifyAdapter from @nestjs/platform-fastify and pass it as the second argument to NestFactory.create().
Question 4: What is the role of the @Injectable() decorator in NestJS?
- Marks a class as an HTTP controller
- Marks a class as a module
- Marks a class as a provider manageable by the DI system (Correct answer)
- Marks a method as a route handler
Correct answer: Marks a class as a provider manageable by the DI system
@Injectable() tells NestJS that this class can be injected as a dependency and managed by the IoC container.
Question 5: Which NestJS concept is analogous to Angular's concept of modules?
- Controllers
- Pipes
- Modules decorated with @Module() (Correct answer)
- Guards
Correct answer: Modules decorated with @Module()
NestJS was heavily inspired by Angular, and both use @Module() to organize code into cohesive feature blocks.
Question 6: What does NestFactory.create() return?
- An Express app instance
- A Promise resolving to an INestApplication instance (Correct answer)
- A module reference
- A controller registry
Correct answer: A Promise resolving to an INestApplication instance
NestFactory.create() is async and returns a Promise<INestApplication>, which provides methods like listen(), use(), and enableCors().
Question 7: What is a 'feature module' in NestJS architecture?
- A module loaded only in production
- A module that groups related controllers, services, and entities for a single domain feature (Correct answer)
- A built-in NestJS utility module
- A module that handles global error handling
Correct answer: A module that groups related controllers, services, and entities for a single domain feature
Feature modules encapsulate all the code for a specific domain (e.g., UsersModule) to keep the codebase organized and maintainable.
Which HTTP adapter does NestJS use by default under the hood?