NestJS interview questions

20 questions from the NestJS: Building Scalable Node.js APIs course, each with the correct answer and an explanation. Try to answer before you open the answer.

Module 1: Getting Started

1. What does `nest g resource users` do that `nest g controller users` does not?

  1. It only creates a service file
  2. It generates module, controller, service and DTOs together
  3. It starts the server in watch mode
Show answer

Answer: It generates module, controller, service and DTOs together

`resource` scaffolds the whole feature slice, while `controller` creates only the controller.

Lesson: Setup with NestJS CLI

2. Which file is the entry point that calls `NestFactory.create()`?

  1. app.module.ts
  2. main.ts
  3. app.service.ts
Show answer

Answer: main.ts

`main.ts` bootstraps the app. `app.module.ts` is only the root module it receives.

Lesson: Project Structure

Module 2: Foundation

3. A provider is listed in `providers` but you inject it in another module and get an error. What is missing?

  1. It must be exported by its module and that module imported
  2. The class needs `@Controller()`
  3. Nest only injects providers named `Service`
Show answer

Answer: It must be exported by its module and that module imported

Providers are private to their module until exported, and the consumer must import that module.

Lesson: Dependency Injection

Module 3: Core Concepts

4. What is the default scope of a Nest provider?

  1. A new instance per request
  2. A new instance per injection
  3. A single shared instance (singleton)
Show answer

Answer: A single shared instance (singleton)

By default Nest creates one instance and reuses it. Request scope must be opted into.

Lesson: Providers & Services

5. In the request lifecycle, which runs first?

  1. Guards
  2. Pipes
  3. Middleware
Show answer

Answer: Middleware

Order is middleware, guards, interceptors (before), pipes, handler.

Lesson: Middleware

6. `ValidationPipe` does not reject unknown properties by default. Which option strips them?

  1. `whitelist: true`
  2. `transform: false`
  3. `skipMissingProperties: true`
Show answer

Answer: `whitelist: true`

`whitelist: true` removes properties without validation decorators; `forbidNonWhitelisted` makes them an error.

Lesson: Pipes & Validation

7. What does `@Catch()` with no arguments on an exception filter catch?

  1. Only HttpException
  2. Every thrown exception
  3. Only validation errors
Show answer

Answer: Every thrown exception

An empty `@Catch()` is a catch-all filter for any exception type.

Lesson: Exception Handling

Module 4: Database & Data

8. Why generate a migration instead of editing the database by hand?

  1. Migrations make queries faster
  2. Changes are versioned, reviewable and repeatable across environments
  3. TypeORM refuses manual SQL
Show answer

Answer: Changes are versioned, reviewable and repeatable across environments

Migration files live in git and run the same way on dev, staging and prod.

Lesson: Migrations

Module 5: Design & Patterns

9. Why create `new Logger(UsersService.name)`?

  1. It adds the class name as the log context
  2. It disables console output
  3. It writes logs to a file
Show answer

Answer: It adds the class name as the log context

The context label shows which class emitted each line.

Lesson: Logging

10. What happens to a `@UseInterceptors(CacheInterceptor)` GET route on the second identical request within the TTL?

  1. The handler runs again and overwrites the cache
  2. The cached response is returned without running the handler
  3. A 304 is always returned
Show answer

Answer: The cached response is returned without running the handler

Cache hits short-circuit the handler. Only GET responses are auto-cached.

Lesson: Caching

Module 6: Practice & Deployment

11. In `Test.createTestingModule`, how do you replace a real service with a fake?

  1. `.overrideProvider(Service).useValue(fake)`
  2. Delete the service file
  3. Set `NODE_ENV=fake`
Show answer

Answer: `.overrideProvider(Service).useValue(fake)`

`overrideProvider` swaps the dependency inside the test module only.

Lesson: Testing

12. Why use a multi-stage Dockerfile for a Nest app?

  1. To run two servers
  2. To compile TypeScript in one stage and ship only dist and prod dependencies
  3. To avoid needing Node
Show answer

Answer: To compile TypeScript in one stage and ship only dist and prod dependencies

The final image skips devDependencies and sources, so it is smaller.

Lesson: Docker Integration

Module 10: Revision & Resources

13. What does a NestJS module do?

  1. Compiles TypeScript
  2. Opens database connections only
  3. Groups related controllers and providers
  4. Replaces Express
Show answer

Answer: Groups related controllers and providers

Modules are the unit of organization in a Nest app.

Lesson: Key Takeaways

14. How are services normally obtained in a controller?

  1. new Service()
  2. Global variables
  3. require() at runtime
  4. Constructor dependency injection
Show answer

Answer: Constructor dependency injection

Nest's IoC container injects providers for you.

Lesson: Key Takeaways

15. Which component validates and transforms incoming data?

  1. Pipe
  2. Guard
  3. Filter
  4. Module
Show answer

Answer: Pipe

ValidationPipe checks DTOs before the handler runs.

Lesson: Key Takeaways

16. Which component decides whether a request may continue (e.g. authorization)?

  1. Pipe
  2. Guard
  3. Interceptor
  4. Entity
Show answer

Answer: Guard

Guards implement CanActivate and allow or reject.

Lesson: Key Takeaways

17. What is the correct order of the request lifecycle?

  1. Pipes, Guards, Middleware, Handler
  2. Handler, Pipes, Guards
  3. Middleware, Guards, Interceptors, Pipes, Handler
  4. Guards, Handler, Middleware
Show answer

Answer: Middleware, Guards, Interceptors, Pipes, Handler

Interceptors run before pipes and again after the handler.

Lesson: Key Takeaways

18. What problem does N+1 describe in ORMs?

  1. Too many migrations
  2. Too many modules
  3. Slow compilation
  4. One query per related row instead of a join or batch
Show answer

Answer: One query per related row instead of a join or batch

Load relations with a join or batch to avoid it.

Lesson: Key Takeaways

19. Which tool versions your database schema in TypeORM?

  1. Migrations
  2. Interceptors
  3. Pipes
  4. Decorators
Show answer

Answer: Migrations

Migrations record and replay schema changes.

Lesson: Key Takeaways

20. Why use a multi-stage Docker build for NestJS?

  1. Faster TypeScript
  2. Smaller final image with only runtime files
  3. It is required by Nest
  4. To skip tests
Show answer

Answer: Smaller final image with only runtime files

Build tools stay in the builder stage; the final image is lean.

Lesson: Key Takeaways

← Back to the course