How josefdri/express-ts-clean-arch works
Project Type and Purpose
• This is a backend‐only, RESTful API service built with Node.js, Express and TypeScript, following Uncle Bob’s Clean Architecture.
• It exposes endpoints under /api/employees to create and list employees, with JWT‐based auth, MongoDB for persistence, and unit tests around the route layer.
File Structure & Architectural Layers
• Core (Domain)
• src/domain/models & src/domain/usecases: Business entities (Employee) and use‐case interfaces (addEmployee, loadEmployees, checkToken).
• Data Layer
• src/data/protocols: Abstractions for cryptography (encrypter/decrypter) and DB repos.
• src/data/usecases: Implementations of use‐cases orchestrating protocols.
• Infra Layer
• src/infra/cryptography/jwt: JWT signing/verification.
• src/infra/db/mongodb: MongoDB employee repository.
• src/infra/usecases: Glue implementations (e.g., checkToken).
• Presentation Layer
• src/presentation/controllers: HTTP controllers handling requests/responses.
• src/presentation/middlewares: Auth middleware.
• Entry Point & Wiring (Main)
• src/main/config: Express app setup, middleware, routes.
• src/main/adapters: Express‐to‐usecase adapters.
• src/main/factories: Composition roots – factories that build controllers, use cases, decorators, middlewares.
• src/main/server.ts: Boots the HTTP server.
• Validation
• src/validation/validators: Request validation logic.
• External Client: Browser/Postman (HTTP requests with JWT).
• API Gateway (Express Server)
• server.ts → app.ts
• Global middleware (logging, bodyParser)
• Routing Layer
• routes/employee.routes maps endpoints to controller factories.
• Middleware
• Auth‐middleware (JWT verification → check-token use case)
• Controllers (Presentation)
• AddEmployeeController & LoadEmployeesController
• Validate input → invoke use case → format HTTP response.
• Use Cases
• Domain interface → Data‐layer implementation
• dbAddEmployee, dbLoadEmployee, checkToken
• Protocols/Repositories
• AddEmployeeRepository, LoadEmployeeRepository abstractions
• MongoEmployeeRepository (implements both)
• Cryptography
• Encrypter & Decrypter interfaces → JwtAdapter
• Database
• MongoDB instance
• Central Domain
• Entities & pure business rules, isolated from infra.
Relationships & Interactions
• HTTP Request → Express → [Global Middleware] → Router
• Router → [Auth Middleware] → Controller
• Controller → Validation → Use Case (injected impl)
• Use Case → Protocols (e.g., repo, crypto)
• Protocol Impl → External resources (MongoDB, JWT lib)
• Response bubbles back through layers.
Architectural Patterns & Principles
• Clean Architecture: Concentric layers, dependencies point inwards.
• Dependency Inversion: Controllers/use‐cases depend on abstractions.
• Factory Pattern: main/factories builds and wires all dependencies.
• Decorator Pattern: Logging decorator wraps controllers.
• Singleton: Shared adapters (e.g., one DB client).
• SOLID: Each layer has a single responsibility; open for extension.