# Key Takeaways — NestJS: Building Scalable Node.js APIs

Source: https://www.skillbyai.com/en/nestjs/nestjs-recap

> Review the key NestJS ideas: modules, controllers, services, dependency injection and the request lifecycle order.

## The NestJS triangle

**Modules** organize your app, **Controllers** handle requests, **Services** contain logic. Everything is tied together by **Dependency Injection**.

## Request lifecycle

Request flow: Middleware → Guards → Interceptors (before) → Pipes → Controller/Handler → Interceptors (after) → Exception filters. Learn this order to debug correctly.

## Database patterns

Use DTOs for input; repositories for data access; migrations for schema. Keep business logic in services, queries in repositories.

## Final quiz 1 of 8

Final quiz

**Quiz:** What does a NestJS module do?

- [ ] Compiles TypeScript
- [ ] Opens database connections only
- [x] Groups related controllers and providers
- [ ] Replaces Express

*Answer:* Groups related controllers and providers. Modules are the unit of organization in a Nest app.

## Final quiz 2 of 8

Final quiz

**Quiz:** How are services normally obtained in a controller?

- [ ] new Service()
- [ ] Global variables
- [ ] require() at runtime
- [x] Constructor dependency injection

*Answer:* Constructor dependency injection. Nest's IoC container injects providers for you.

## Final quiz 3 of 8

Final quiz

**Quiz:** Which component validates and transforms incoming data?

- [x] Pipe
- [ ] Guard
- [ ] Filter
- [ ] Module

*Answer:* Pipe. ValidationPipe checks DTOs before the handler runs.

## Final quiz 4 of 8

Final quiz

**Quiz:** Which component decides whether a request may continue (e.g. authorization)?

- [ ] Pipe
- [x] Guard
- [ ] Interceptor
- [ ] Entity

*Answer:* Guard. Guards implement CanActivate and allow or reject.

## Final quiz 5 of 8

Final quiz

**Quiz:** What is the correct order of the request lifecycle?

- [ ] Pipes, Guards, Middleware, Handler
- [ ] Handler, Pipes, Guards
- [x] Middleware, Guards, Interceptors, Pipes, Handler
- [ ] Guards, Handler, Middleware

*Answer:* Middleware, Guards, Interceptors, Pipes, Handler. Interceptors run before pipes and again after the handler.

## Final quiz 6 of 8

Final quiz

**Quiz:** What problem does N+1 describe in ORMs?

- [ ] Too many migrations
- [ ] Too many modules
- [ ] Slow compilation
- [x] One query per related row instead of a join or batch

*Answer:* One query per related row instead of a join or batch. Load relations with a join or batch to avoid it.

## Final quiz 7 of 8

Final quiz

**Quiz:** Which tool versions your database schema in TypeORM?

- [x] Migrations
- [ ] Interceptors
- [ ] Pipes
- [ ] Decorators

*Answer:* Migrations. Migrations record and replay schema changes.

## Final quiz 8 of 8

Final quiz

**Quiz:** Why use a multi-stage Docker build for NestJS?

- [ ] Faster TypeScript
- [x] Smaller final image with only runtime files
- [ ] It is required by Nest
- [ ] To skip tests

*Answer:* Smaller final image with only runtime files. Build tools stay in the builder stage; the final image is lean.
