NestJS Architecture

Inspired by Angular and Spring

NestJS borrows ideas from Angular (modules, decorators, dependency injection) and Spring Boot (controllers, services, layered architecture). If you know Spring, NestJS will feel familiar.

NestJSSpring Boot equivalentJob
@Module()@Configuration / packageGroup related code
@Controller()@RestControllerHandle HTTP requests
@Injectable() service@ServiceBusiness logic
Repository (TypeORM/Prisma)JpaRepositoryDatabase access
GuardSpring Security filterAllow or deny a request
PipeBean ValidationValidate and convert input

The Three Building Blocks

AppModule (root)
                 /             \
        ProductsModule      OrdersModule        <- modules group features
           |      \            |      \
 ProductsController  ProductsService ...        <- controllers: HTTP layer
                       |                           providers: business logic
                 ProductsRepository             <- data access

Request:  client --> Controller --> Service --> Repository --> Database
  • Module: a box that groups a feature (products, orders, users).
  • Controller: receives a request and returns a response. It should be thin.
  • Provider (usually a service): does the real work. NestJS creates it and injects it wherever it is needed.