NestJS Junior interview questions and answers

75 questions for a NestJS (Junior) interview - with correct answers, explanations and links to the docs.

Take this quiz
  1. 1.What is NestJS?
  2. 2.Which HTTP platform does NestJS use by default?
  3. 3.How do you create a new NestJS project?
  4. 4.What does main.ts usually contain in a Nest app?
  5. 5.What language does NestJS primarily use?
  6. 6.Which three building blocks make up most of a Nest feature?
  7. 7.What does the nest g resource users command do?
  8. 8.Why does Nest use decorators like @Controller() and @Injectable()?
  9. 9.What does the @Module() decorator describe?
  10. 10.OrdersModule needs UsersService from UsersModule. What must be true?
  11. 11.What does @Global() on a module do?
  12. 12.Are module providers singletons by default?
  13. 13.Why split an application into feature modules?
  14. 14.What is a typical way to load environment configuration in Nest?
  15. 15.What is the role of a controller?
  16. 16.Which route does this handler serve?
  17. 17.How do you read the JSON body of a POST request in a controller?
  18. 18.What status code does a @Post() handler return by default?
  19. 19.What happens if a controller method returns a plain object?
  20. 20.What is a DTO in Nest?
  21. 21.Why should a DTO be a class rather than a TypeScript interface?
  22. 22.How do you read ?page=2 in a controller?
  23. 23.What is a provider in Nest?
  24. 24.How does a controller get a service instance?
  25. 25.What is the main benefit of dependency injection?
  26. 26.What does @Injectable() mark?
  27. 27.What does the error 'Nest can't resolve dependencies of the UsersController (?)' usually mean?
  28. 28.How do you register a plain value as a provider, e.g. a config object?
  29. 29.What does a pipe do in Nest?
  30. 30.What does @Param('id', ParseIntPipe) id: number do with /users/abc?
  31. 31.How is request body validation usually set up?
  32. 32.What does the whitelist: true option of ValidationPipe do?
  33. 33.What does transform: true in ValidationPipe give you?
  34. 34.How do you return a 404 from a service or controller?
  35. 35.What does Nest return if a handler throws a plain Error?
  36. 36.Which built-in exceptions map to 400, 401 and 403?
  37. 37.What is an exception filter?
  38. 38.What is middleware in Nest?
  39. 39.What is the job of a guard?
  40. 40.How do you protect a route with a guard?
  41. 41.What can an interceptor do?
  42. 42.Where do you configure CORS in a Nest app?
  43. 43.Where should business logic live in a Nest app?
  44. 44.Which ORMs and database tools are commonly used with Nest?
  45. 45.How is TypeORM typically connected to a Nest app?
  46. 46.How do you use Prisma in Nest?
  47. 47.Why are async methods common in Nest services?
  48. 48.How do you create a testing module in Nest?
  49. 49.How do you replace a real service with a mock in a Nest test?
  50. 50.What does an e2e test in a Nest project typically do?
  51. 51.Which test runner does the default Nest project use?
  52. 52.How is JWT authentication often implemented in Nest?
  53. 53.How can you mark some routes as public when an auth guard is global?
  54. 54.How do you add rate limiting to a Nest API?
  55. 55.Why should password hashes never be returned in API responses?
  56. 56.How do you generate OpenAPI (Swagger) docs in Nest?
  57. 57.What does app.setGlobalPrefix('api') do?
  58. 58.Which built-in logger does Nest provide?
  59. 59.How do you run a method on a schedule, e.g. every night?
  60. 60.What is @nestjs/terminus used for?
  61. 61.How do you handle file uploads in a Nest app on Express?
  62. 62.What does ClassSerializerInterceptor do together with class-transformer?
  63. 63.Which lifecycle hook runs once a module's dependencies are resolved?
  64. 64.What must you call so that shutdown hooks run on SIGTERM?
  65. 65.How is a typical Nest project organized?
  66. 66.What does @Res() in a handler change?
  67. 67.Can Nest serve GraphQL instead of REST?
  68. 68.What does nest start --watch do?
  69. 69.How do you send a custom header from a handler without using @Res()?
  70. 70.What is the default TypeScript setting Nest projects need for DI by type?
  71. 71.What does PartialType(CreateUserDto) give you?
  72. 72.What does @Query('limit', new DefaultValuePipe(20), ParseIntPipe) limit: number do when limit is missing?
  73. 73.How do you call an external HTTP API from a Nest service in the Nest way?
  74. 74.Why use TypeOrmModule.forRootAsync({ useFactory, inject: [ConfigService] }) instead of forRoot({...})?
  75. 75.Why prefer @Body(), @Param() and @Query() over @Req() in handlers?