NestJS Middle interview questions and answers
75 questions for a NestJS (Middle) interview - with correct answers, explanations and links to the docs.
Take this quiz- 1.In what order do Nest's request-processing layers run?
- 2.Why should authentication be a guard rather than middleware?
- 3.What is
ExecutionContextused for? - 4.How does a roles guard read roles declared on a route?
- 5.How do you register a global guard that can inject other providers?
- 6.What does an interceptor's
intercept(context, next)return? - 7.How can an interceptor wrap every response in
{ data: ... }? - 8.How can an interceptor add a timeout to slow handlers?
- 9.How do you write a custom parameter decorator like
@User()? - 10.How do you combine several decorators into one, e.g. guards plus Swagger annotations?
- 11.When would you use
useFactoryfor a provider? - 12.What is
useExistingfor? - 13.Why inject an interface-like dependency by token instead of by class?
- 14.What is a dynamic module?
- 15.What's the usual convention for
forRoot,registerandforFeature? - 16.What are the three injection scopes in Nest?
- 17.What is a side effect of making a provider request-scoped?
- 18.How do you resolve a circular dependency between two providers?
- 19.What does
ModuleReflet you do? - 20.Which lifecycle hook is the right place to open a connection that other modules rely on during startup?
- 21.How do you validate nested objects in a DTO?
- 22.What is a risk of using database entities directly as request DTOs?
- 23.How can a custom validation rule check something asynchronous, like 'email is not taken'?
- 24.Why might you write a custom pipe instead of using
ValidationPipe? - 25.What does
@Expose({ groups: ['admin'] })withClassSerializerInterceptorallow? - 26.How do you write a global filter that formats all errors as RFC 9457 problem details?
- 27.Where should database errors like unique violations be translated into HTTP errors?
- 28.Why can exception filters behave differently in microservices and HTTP?
- 29.How does Passport fit into Nest authentication?
- 30.How do you implement permission rules more fine-grained than roles, e.g. 'users can edit only their own posts'?
- 31.Why check object ownership in addition to authentication?
- 32.How do you run several TypeORM operations in one transaction in Nest?
- 33.Why is
synchronize: truein TypeORM dangerous in production? - 34.What's a clean way to keep Prisma or TypeORM details out of business logic?
- 35.How do you cache GET responses in Nest?
- 36.How do you move slow work, like sending emails, to a background queue in Nest?
- 37.What does
@nestjs/event-emitterprovide? - 38.How do you push live updates to the browser over HTTP in Nest?
- 39.What is a WebSocket gateway in Nest?
- 40.What's the difference between unit and e2e tests in a Nest project?
- 41.In an e2e test, why apply the same global pipes and filters as in
main.ts? - 42.How do you override a guard in a test to simulate an authenticated user?
- 43.Why is it useful to test against a real database in Docker for repository code?
- 44.What's the difference between
@MessagePatternand@EventPatternin Nest microservices? - 45.Which transports do Nest microservices support out of the box?
- 46.What is a hybrid application in Nest?
- 47.What's the difference between code-first and schema-first GraphQL in Nest?
- 48.What does
@ResolveField()do in a GraphQL resolver? - 49.How do you validate environment variables at startup with
@nestjs/config? - 50.How do you organize configuration into typed namespaces?
- 51.Which API versioning strategies does Nest support?
- 52.What does the
@nestjs/swaggerCLI plugin do? - 53.Why might you switch a Nest app from Express to Fastify?
- 54.What problem can a 'shared module' that exports everything cause?
- 55.What is a Nest monorepo (workspace) used for?
- 56.What is a standalone Nest application?
- 57.How can you read the current request ID in deep services without passing it through every call?
- 58.How do you enable graceful shutdown with health checks in Kubernetes for Nest?
- 59.What is middleware bound in
configure(consumer)NOT able to see? - 60.How do you exclude some routes from a middleware?
- 61.What does
@nestjs/cqrsprovide? - 62.How do you compress responses in Nest?
- 63.What's a sign that a Nest controller is doing too much?
- 64.How do you set security headers in a Nest app?
- 65.What does
@nestjs/scheduleoffer besides@Cron? - 66.How can you log every request with timing in Nest?
- 67.Why might
ValidationPipenot reject unknown query parameters by default? - 68.What does
@Controller({ path: 'users', version: '2' })require to work? - 69.What's the purpose of
nest buildwith webpack or SWC in production? - 70.After upgrading to NestJS 11 (Express 5), a route
@Get('files/*')stops matching as before. Why? - 71.With several app instances, why does the default throttler storage give wrong limits?
- 72.Which built-in pipe rejects a route param that isn't a valid UUID?
- 73.When are lazy-loaded modules (
LazyModuleLoader) useful? - 74.Interceptors are bound globally, on a controller and on a route. In what order does their 'after' logic run?
- 75.What does
@Optional()on a constructor parameter do?