DEV Community

codingKrills
codingKrills

Posted on

Nestjs Questions & Answeres.

NestJS Interview Questions & Answers

1. What is NestJS and why use it?

NestJS is a progressive Node.js framework built on TypeScript for
building scalable and maintainable server-side applications. It
provides modularity, dependency injection, and decorators, making it
structured like Angular.


2. What are modules in NestJS?

  • A module is a container for a feature (controllers, providers, services).
  • Every NestJS app has at least one root module (AppModule).
  • Helps in organizing code into modular, reusable units.

3. What are controllers in NestJS?

Controllers handle incoming HTTP requests and return responses.\
Defined with @Controller() decorator and route handlers with @Get(),
@Post(), etc.


4. What are providers/services in NestJS?

  • Services (providers) hold business logic.
  • They can be injected into controllers or other providers using dependency injection.
  • Decorated with @Injectable().

5. What is Dependency Injection in NestJS?

A design pattern where classes request dependencies from the framework
rather than creating them manually.\
Example: Injecting UsersService into UsersController instead of
instantiating it.


6. What are decorators in NestJS?

Special functions that add metadata.\
Examples:\

  • @Controller() -- marks a class as controller\
  • @Get() -- maps a GET request\
  • @Injectable() -- makes a service available for DI

7. What are pipes in NestJS?

  • Pipes are used for validation and transformation.
  • Example: ValidationPipe automatically validates DTOs against class validators.

8. What are interceptors in NestJS?

  • Interceptors can transform requests/responses, log execution time, handle caching, etc.
  • Example: LoggingInterceptor to measure API execution time.

9. What are guards in NestJS?

  • Guards control authorization.
  • Example: AuthGuard checks if the user is authenticated before accessing a route.

10. What are filters in NestJS?

  • Exception filters catch unhandled exceptions and format error responses.
  • Example: HttpExceptionFilter to customize error messages.

11. What is Middleware in NestJS?

  • Middleware runs before route handlers.
  • Used for logging, authentication, parsing, etc.
  • Example: app.use(LoggerMiddleware)

12. How does NestJS handle database integration?

  • Via TypeORM, Sequelize, Prisma, or Mongoose.
  • Use repositories or models injected into services to interact with DB.

13. What are DTOs in NestJS?

  • DTO = Data Transfer Object.
  • Defines the shape of data received or sent.
  • Used with validation decorators like @IsString(), @IsEmail().

14. What is the difference between forRoot() and forFeature() in modules?

  • forRoot() → used in the root module to configure a module globally (e.g., database connection).
  • forFeature() → used in feature modules to register entities or repositories.

15. How does NestJS support Microservices?

  • Provides @nestjs/microservices package.
  • Supports TCP, Redis, Kafka, RabbitMQ, NATS, and gRPC.
  • Uses message-based communication instead of HTTP.

16. What is the difference between @Module() imports, providers, controllers, and exports?

  • imports → modules to import\
  • controllers → handle requests\
  • providers → services/business logic\
  • exports → services to share with other modules

17. How does NestJS handle authentication?

  • Using Passport.js strategy integration (@nestjs/passport).
  • JWT-based authentication is most common.
  • Guard + Strategy pattern (JwtAuthGuard).

18. What is the lifecycle of a request in NestJS?

Middleware → Guards → Interceptors (before) → Pipes → Controller →
Service → Interceptors (after) → Response → Filters (if error).


19. What is the difference between Monorepo and Microservice architecture in NestJS?

  • Monorepo: All apps/services in a single repo with shared code.
  • Microservice: Independent services communicating via events/messages.
  • NestJS supports both.

20. How do you implement caching in NestJS?

  • Use @nestjs/cache-manager or Redis.
  • Example: CacheInterceptor to cache responses.
  • Can also use manual caching with cacheManager.set() and cacheManager.get().

Top comments (0)