A maintainable, scalable, and enterprise-ready backend template built with Node.js and Express.js, following Clean Architecture principles and Modular Design.
The project is divided into four main layers to ensure separation of concerns and testability:
- Domain Layer: Contains Entities and Repository Interfaces (Pure Business Logic).
- Application Layer: Contains Use Cases and DTOs (Orchestrates Business Flow).
- Infrastructure Layer: Technical implementations (Prisma, Security, Services).
- Interface Layer: Web layer containing Controllers and Routes.
- Modular Monolith: Features are encapsulated in independent modules.
- Dependency Injection: Managed via Awilix for automatic wiring.
- Repository Pattern: Abstraction of data access.
- Middleware-based Validation: Request validation using Joi.
- Core: Node.js (ESM), Express.js
- Database & ORM: PostgreSQL/MySQL, Prisma
- DI Container: Awilix
- Authentication: JWT (jsonwebtoken), Bcrypt
- Security: Helmet, CORS
- Logging: Winston & Winston Daily Rotate File
- Validation: Joi
- Documentation: Swagger UI (swagger-jsdoc)
- Testing: Jest, Supertest
- Node.js >= 18.x
- NPM or Yarn
- Database (PostgreSQL/MySQL)
- Clone the repository:
git clone <repository-url> cd start-api-expresjs
- Install dependencies:
npm install
- Setup Environment Variables:
cp .env.example .env # Update JWT_SECRET and DATABASE_URL in .env
# Generate Prisma Client
npx prisma generate
# Sync Database Schema
npx prisma db push# Development mode
npm run dev
# Production mode
npm run startDocumentation is automatically generated and available via Swagger UI.
- Swagger URL:
http://localhost:3000/api-docs
| Module | Method | Endpoint | Description | Auth |
|---|---|---|---|---|
| Auth | POST | /auth/login |
Login to get JWT Token | Public |
| User | GET | /users/auth/me |
Get current user profile | Private |
| User | GET | /users |
List all users (Paginated) | Private |
| User | POST | /users |
Create new user | Private |
| User | GET | /users/:idPersonal |
Get user by Personal ID | Private |
| User | PUT | /users/:id |
Update user data | Private |
| User | DELETE | /users/:id |
Delete user | Private |
| Health | GET | /health |
Check system health | Public |
# Run all tests
npm test
# Run specific test file
npm test tests/integration/auth.integration.test.jssrc/
βββ modules/ # Domain-driven modules (Authentication, User, etc.)
βββ infrastructure/ # Global external services & database config (Prisma client, Logger)
βββ shared/ # Shared utilities, errors, and middleware
βββ config/ # App configurations (Env, Logger)
βββ container.js # Dependency Injection Registry (Awilix)
βββ app.js # Express App Setup
βββ server.js # Application Entry Point
Every new module (e.g., Product, Order) should follow this standard structure to maintain Clean Architecture principles:
src/modules/[module_name]/
βββ domain/ # Blueprints & Business Rules (Pure Logic)
β βββ entities/ # Business objects/models
β βββ repositories/ # Repository Interfaces (Contracts)
β βββ services/ # (Optional) Cross-entity domain logic
β
βββ application/ # Orchestration & Use Cases
β βββ usecases/ # One file per action (e.g., CreateUserUseCase.js)
β βββ dtos/ # Data Transfer Objects (Input/Output structures)
β
βββ infrastructure/ # Technical Details & Implementation
β βββ repositories/ # Repository Implementations (e.g., Prisma Repository)
β βββ validation/ # Input validation schemas (Joi)
β βββ security/ # Module-specific security (Hashing, etc.)
β βββ services/ # External service implementations
β
βββ interfaces/ # Entry Points (Web/API)
βββ controllers/ # HTTP Request/Response handling
βββ routes/ # API Endpoint definitions
- Domain: Define the business rules and repository contracts.
- Infrastructure: Implement the repository contracts (e.g., using Prisma).
- Application: Create use cases to orchestrate business logic.
- Interfaces: Define controllers and routes to expose the functionality.
- Registration:
- DI: Awilix automatically resolves dependencies. Ensure your classes are exported correctly.
- Express: Register the new routes in
src/app.js.
Tip
For a detailed step-by-step example and code snippets, see the Module Development Flow Guide.
- One Use Case per File: Keeps code focused, testable, and maintainable.
- DTOs for Input: Never pass raw
reqobjects into use cases. Use DTOs to structure data. - Dependency Inversion: High-level modules (Use Cases) should not depend on low-level modules (Prisma). Both should depend on abstractions (Repository Interfaces).
This project uses GitHub Actions to ensure code quality and stability. Every push or pull request to the main branch triggers the CI pipeline, which performs the following:
- Environment Setup: Uses Node.js 20.x and a PostgreSQL 15 service container.
- Dependency Check: Runs
npm cifor clean and repeatable installations. - Prisma Validation: Validates the database schema and generates the Prisma client.
- Dependency Integrity: Uses
dependency-cruiserto validate module boundaries. - Automated Testing: Runs unit and integration tests with Jest.
This project is licensed under the MIT License - see the LICENSE file for details.