Lesson 3 / 34

Project Structure

Organize a NestJS app into feature folders with a module, controller and service for each feature.

Folder layout

Each feature lives in its own folder with its module, controller, and service.

src/
  main.ts
  app.module.ts
  users/
    users.module.ts
    users.controller.ts
    users.service.ts
  posts/
    posts.module.ts
    posts.controller.ts
    posts.service.ts

Module = a slice of the app

A module groups related controllers and services. The root AppModule imports all feature modules; the app assembles them into a tree.

Group by feature, not by type

Keep users/users.controller.ts, users.service.ts and users.module.ts together in one users/ folder. Do not create global controllers/ and services/ folders; they grow into a maze.

Quick check

Quick check: Which file is the entry point that calls `NestFactory.create()`?

  • app.module.ts
  • main.ts
  • app.service.ts
Answer

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