NestJS project structure.
A NestJS app is a tree of modules. The root module imports one module per feature, and each feature keeps its controller, service and data shapes in its own folder. This page lays out that structure, then shows a real open-source NestJS API mapped by Nodeway.
The conventional layout
What the Nest CLI creates with nest new and nest generate resource users:
- src/main.tsStarts the app with NestFactory.create and listens on a port.
- src/app.module.tsThe root module. It imports every feature module.
- src/users/One folder per feature.
- users.module.tsDeclares the feature's controllers and providers.
- users.controller.tsRoutes: @Controller('users') with @Get, @Post and the rest.
- users.service.tsThe work behind the routes, injected into the controller.
- dto/Request body shapes, often validated with class-validator.
- entities/The data model, such as TypeORM entities.
- test/End-to-end tests. Unit tests sit beside the code as *.spec.ts.
- nest-cli.jsonSettings for the Nest CLI.
How the pieces connect
- A request reaches a controller method through its route decorators. The method calls a service, and the service talks to the database.
- Services are providers: Nest creates them and passes them into the constructors that ask for them. A module has to list a provider, or import the module that exports it, before its classes can use it.
- Cross-cutting pieces, such as guards, pipes, interceptors and filters, often get a
commonorsharedfolder.
A real example: NestJS RealWorld example
lujakob/nestjs-realworld-example-app is a RealWorld backend API built with NestJS and TypeORM. Nodeway mapped it at commit c1c2cc4, from 18 January 2021. Most of it is TypeScript: 34 files of 35, followed by JavaScript (1).
It follows the feature-folder layout: user, article, profile and tag each hold their own module, controller, service and entities.
- Source files
- 35
- Functions and classes
- 83
- Confirmed calls
- 42
- Routes
- 21
- Tables
- 5
Its folders
- src/34 files21 routes · 5 tables
- user/11 files5 routes · 1 table
- article/9 files11 routes · 2 tables
- profile/5 files3 routes · 1 table
- tag/4 files1 route · 1 table
- shared/2 files
Routes
Nodeway found 21 routes and linked 21 to the function that handles them. They by HTTP method: GET 8, POST 6, DELETE 5, PUT 2. Most are under src/article (11), src/user (5) and src/profile (3).
Some of them
| Route | Method |
|---|---|
| /root · src/app.controller.ts | GET |
| /tagsfindAll · src/tag/tag.controller.ts | GET |
| /userfindMe · src/user/user.controller.ts | GET |
| /articlesfindAll · src/article/article.controller.ts | GET |
| /profiles/:usernamegetProfile · src/profile/profile.controller.ts | GET |
| /userupdate · src/user/user.controller.ts | PUT |
| /userscreate · src/user/user.controller.ts | POST |
| /articlescreate · src/article/article.controller.ts | POST |
Data model
Nodeway found 5 database tables, defined with TypeORM. Code in the repository reads or writes 5 of them. The most used:
| Table | Used by |
|---|---|
| user6 columns · links to article · src/user/user.entity.ts | 15 read6 write |
| article11 columns · links to comment and user · src/article/article.entity.ts | 9 read7 write |
| follows3 columns · src/profile/follows.entity.ts | 3 read2 write |
| comment3 columns · links to article · src/article/comment.entity.ts | 1 read2 write |
| tag2 columns · src/tag/tag.entity.ts | 1 read0 write |
The full map, with its entry points, a traced request and the most-called code, is on the NestJS RealWorld example architecture page.
See your own project's structure
Nodeway reads a NestJS project on your own computer and draws its folders, files, functions, routes and tables, with the imports and calls between them. Open a folder and the map is drawn locally; nothing is uploaded. The getting started guide covers installing it, and the repo maps show 31 open-source projects mapped the same way.
Questions
What is the difference between a controller and a service in NestJS?
A controller receives requests: its decorators, such as @Get and @Post, map routes to methods. A service holds the work those methods do and is injected into the controller through its constructor, so the same logic can be reused and tested on its own.
Should NestJS folders be organised by feature or by type?
The Nest CLI and documentation organise by feature: a users folder holds the users module, controller, service and DTOs together. Folders by type, such as one controllers folder for everything, work too but spread one feature across many places.
What does nest generate resource create?
A feature folder with a module, a controller, a service, create and update DTOs and an entity, already wired into each other.