NestJS Answers
0:000:00
NestJS Answers
Basic NestJS Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | What is NestJS? | A progressive Node.js framework for building efficient, scalable server-side applications | Inspired by Angular, uses TypeScript by default |
2 | Why use NestJS? | Provides structure, modularity, and maintainability for Node.js applications | Encourages best practices, dependency injection, decorators, CLI tools |
3 | What is a Module? | A fundamental building block in NestJS; groups related components | @Module({ controllers: [], providers: [] }) export class AppModule {} |
4 | What is a Controller? | A class that handles incoming requests and returns responses | @Controller('users') export class UsersController { ... } |
5 | What is a Provider? | A class that encapsulates logic and can be injected (often services) | @Injectable() export class UsersService { ... } |
6 | What is a Decorator? | A special syntax that adds metadata to classes, methods, properties, etc. | @Controller(), @Injectable(), @Get(), @Post(), @Module() |
7 | What is Dependency Injection (DI)? | A design pattern where a framework "injects" dependencies into components | constructor(private readonly usersService: UsersService) {} |
8 | How do you create a new NestJS project? | Using the Nest CLI | nest new my-app |
9 | How do you generate a Controller? | Using the Nest CLI | nest generate controller users |
10 | How do you generate a Service? | Using the Nest CLI | nest generate service users |
11 | What is the @Get() decorator? | Defines a route handler for HTTP GET requests | @Get() findAll(): string { ... } |
12 | What is the @Post() decorator? | Defines a route handler for HTTP POST requests | @Post() create(@Body() createUserDto: CreateUserDto) { ... } |
13 | What is the @Body() decorator? | Extracts parameters from the request body | @Post() create(@Body() createUserDto: CreateUserDto) { ... } |
14 | What is the @Param() decorator? | Extracts route parameters from the URL | @Get(':id') findOne(@Param('id') id: string) { ... } |
15 | What is the @Query() decorator? | Extracts query parameters from the URL | @Get() findAll(@Query('name') name: string) { ... } |
Intermediate NestJS Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | What is the imports array in a Module? | Specifies other modules that the current module needs to use | @Module({ imports: [HttpModule] }) export class UsersModule {} |
2 | What is the providers array in a Module? | Lists services or other providers that are created and managed by the module | @Module({ providers: [UsersService] }) export class UsersModule {} |
3 | What is the exports array in a Module? | Specifies providers that should be made available for use in other modules | @Module({ providers: [UsersService], exports: [UsersService] }) export class UsersModule {} |
4 | What is a DTO (Data Transfer Object)? | A class or interface for transferring data between layers (e.g., controller to service) | class CreateUserDto { name: string; email: string; } |
5 | What are Pipes? | Functions that transform incoming data before it's handled by a route handler | @UsePipes(new ValidationPipe()) @Post() create(@Body() createUserDto: CreateUserDto) { ... } |
6 | What are Guards? | Functions that determine whether a route handler should be executed (authorization) | @UseGuards(AuthGuard) @Get('profile') getProfile() { ... } |
7 | What are Interceptors? | Functions that intercept incoming requests and outgoing responses, allowing modification or logging | @UseInterceptors(LoggingInterceptor) @Get() findAll() { ... } |
8 | What are Exception Filters? | Functions that catch unhandled exceptions and provide a consistent error response | @UseFilters(new HttpExceptionFilter()) @Get('error') getError() { ... } |
9 | What is the Scope of a provider? | Defines the lifecycle of a provider instance (e.g., Singleton, Request) | @Injectable({ scope: Scope.REQUEST }) export class RequestScopedService { ... } |
10 | What is the HttpModule? | A built-in module for making HTTP requests (e.g., to external APIs) | @Module({ imports: [HttpModule] }) export class DataModule {} |
11 | What is the HttpService? | The service provided by HttpModule for making HTTP requests | constructor(private readonly httpService: HttpService) {} |
12 | How do you handle environment variables in NestJS? | Using the ConfigModule from @nestjs/config | @Module({ imports: [ConfigModule.forRoot()] }) export class AppModule {} |
13 | What is the ConfigModule? | A built-in module for managing configuration settings | @Module({ imports: [ConfigModule.forRoot()] }) export class AppModule {} |
14 | What is the ConfigService? | The service provided by ConfigModule to access configuration values | constructor(private readonly configService: ConfigService) {} |
15 | What is the concept of Microservices in NestJS? | Building applications as small, independent services that communicate over a network | Using @nestjs/microservices with transport layers like TCP, RabbitMQ, Kafka, gRPC |
Advanced NestJS Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | What is the ValidationPipe? | A built-in pipe for validating incoming request data based on types (e.g., DTOs) | @UsePipes(new ValidationPipe()) @Post(...) |
2 | What is ClassSerializerInterceptor? | A built-in interceptor for transforming class instances into plain JSON objects | @UseInterceptors(new ClassSerializerInterceptor(app.get(Reflector))) |
3 | What is NestFactory? | Provides methods to create a Nest application instance | const app = await NestFactory.create(AppModule); |
4 | What is the Reflector? | Used to retrieve metadata attached to classes or methods (e.g., for Guards, Interceptors) | constructor(private reflector: Reflector) {} |
5 | How do you implement authentication in NestJS? | Using Passport (e.g., passport-local, passport-jwt) and Guards (e.g., AuthGuard('jwt')) | @Module({ imports: [PassportModule, JwtModule.register({...})] }) and @UseGuards(AuthGuard('jwt')) @Get('profile') |
6 | What is a WebSocket Gateway? | A class that handles WebSocket connections and real-time communication | @WebSocketGateway() export class MyGateway { ... } |
7 | What is the @SubscribeMessage() decorator? | Defines a handler for specific WebSocket message events | @SubscribeMessage('events') handleEvent(@MessageBody() data: string) { ... } |
8 | How do you handle database connections in NestJS? | Using ORMs like TypeORM, Sequelize, Mongoose, or direct database drivers | @Module({ imports: [TypeOrmModule.forRoot(...), TypeOrmModule.forFeature([User])] }) |
9 | What is TypeOrmModule? | A NestJS module that integrates with TypeORM, an Object-Relational Mapper | @Module({ imports: [TypeOrmModule.forRoot(...)] }) |
10 | What is MongooseModule? | A NestJS module that integrates with Mongoose, an ODM (Object Data Modeling) library | @Module({ imports: [MongooseModule.forRoot(...), MongooseModule.forFeature([{ name: User.name, schema: UserSchema }])] }) |
11 | What is the concept of a "Factory Provider"? | A provider that wraps a dependency that is not a custom class (e.g., from a third-party library) | @Module({ providers: [{ provide: 'Logger', useFactory: () => createLogger() }] }) |
12 | What is a Global Module? | A module whose providers are automatically available in all other modules without explicit import | @Global() @Module({ providers: [SharedService] }) export class SharedModule {} |
13 | How do you handle logging in NestJS? | Using the built-in Logger class or integrating with libraries like Winston or Pino | private readonly logger = new Logger(MyClass.name); logger.log('message'); |
14 | What is the purpose of CommandModule? | Allows creating command-line interfaces using NestJS | @nestjs/command |
15 | How do you deploy a NestJS application? | Using platforms like Docker, Heroku, AWS, Google Cloud, Vercel, Netlify | Building the application (npm run build), creating a Docker image, deploying to a cloud provider. |