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