You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A Node.js REST API for a digital marketplace, structured according to Uncle Bob's Clean Architecture principles. This project demonstrates separation of concerns, testability, and scalability by organizing code into distinct layers: Enterprise Business Rules, Application Business Rules, Interface Adapters, and Frameworks & Drivers.
7
+
**Objective:**
8
+
9
+
> This project demonstrates how to apply Uncle Bob's Clean Architecture principles in a Node.js REST API. It is designed as an educational resource to help developers structure their projects for maximum testability, maintainability, and scalability. The codebase shows how to keep business logic independent from frameworks, databases, and delivery mechanisms.
-**Jest** & **Supertest** for unit and integration testing
16
+
-**ESLint** & **Prettier** for linting and formatting
17
+
-**Docker** & **Docker Compose** for containerization
18
+
-**GitHub Actions** for CI/CD
24
19
25
-
## Introduction
20
+
## Why Clean Architecture?
26
21
27
-
This backend API allows users to register, authenticate, and interact with products, blogs, and ratings. It is designed for maintainability and extensibility, following Clean Architecture best practices.
22
+
-**Separation of Concerns:** Each layer has a single responsibility and is independent from others.
23
+
-**Dependency Rule:** Data and control flow from outer layers (e.g., routes/controllers) to inner layers (use cases, domain), never the reverse. Lower layers are unaware of upper layers.
24
+
-**Testability:** Business logic can be tested in isolation by injecting dependencies (e.g., mock DB handlers) from above. No real database is needed for unit tests.
25
+
-**Security & Flexibility:** Infrastructure (DB, frameworks) can be swapped without touching business logic.
28
26
29
-
## Architecture Overview
27
+
> **✨ Ultimate Flexibility:**
28
+
> This project demonstrates that your core business logic is never tied to any specific framework, ORM, or database. You can switch from Express to Fastify, MongoDB to PostgreSQL, or even move to a serverless environment—without rewriting your business rules. The architecture ensures your codebase adapts easily to new technologies, making future migrations and upgrades painless. This is true Clean Architecture in action: your app’s heart beats independently of any tool or vendor.
30
29
31
-
The project is organized into the following layers:
30
+
## How Testing Works
32
31
33
-
-**Enterprise Business Rules**: Core business logic and domain models (`enterprise-business-rules/`).
34
-
-**Application Business Rules**: Use cases and application-specific logic (`application-business-rules/`).
35
-
-**Interface Adapters**: Controllers, database access, adapters, and middlewares (`interface-adapters/`).
36
-
-**Frameworks & Drivers**: Express.js, MongoDB, and other external libraries.
32
+
-**Unit tests** inject mocks for all dependencies (DB, loggers, etc.) into use cases and controllers. This means you can test all business logic without a real database or server.
33
+
-**Integration tests** can use a real or in-memory database, but the architecture allows you to swap these easily.
34
+
-**Example:**
35
+
- The product use case receives a `createProductDbHandler` as a parameter. In production, this is the real DB handler; in tests, it's a mock function.
36
+
- Lower layers (domain, use cases) never import or reference Express, MongoDB, or any framework code.
validate-models/ # Validation logic for domain models
42
44
application-business-rules/
43
-
use-cases/ # Application use cases (products, user)
45
+
use-cases/ # Application use cases (products, user, blog)
44
46
interface-adapters/
45
-
controllers/ # Route controllers for products, users
47
+
controllers/ # Route controllers for products, users, blogs
46
48
database-access/ # DB connection and data access logic
47
49
adapter/ # Adapters (e.g., request/response)
48
50
middlewares/ # Auth, logging, error handling
49
51
routes/ # Express route definitions
50
52
public/ # Static files and HTML views
51
53
```
52
54
53
-
## Features
54
-
55
-
- User registration and authentication (JWT)
56
-
- Product CRUD operations
57
-
- Blog and rating management
58
-
- Role-based access control (admin, blocked users)
59
-
- Input validation and error handling
60
-
- Modular, testable codebase
61
-
62
-
## Stack
63
-
- Express.js
64
-
- Javascript
65
-
- MongoDB doker image
66
-
- Jest
67
-
- Mongo-client + Mongosh
68
-
69
55
## Getting Started
70
56
71
57
### Prerequisites
@@ -84,10 +70,10 @@ public/ # Static files and HTML views
84
70
```bash
85
71
yarn install
86
72
```
87
-
3. Create a `.env` file in the root with your environment variables (see `.env.example` if available):
73
+
3. Create a `.env` file in the root with your environment variables:
88
74
```env
89
75
PORT=5000
90
-
MONGODB_URI=mongodb://localhost:27017/your-db
76
+
MONGO_URI=mongodb://localhost:27017/your-db
91
77
JWT_SECRET=your_jwt_secret
92
78
```
93
79
4. Start the server:
@@ -97,49 +83,37 @@ public/ # Static files and HTML views
97
83
yarn start
98
84
```
99
85
100
-
The server will run at [http://localhost:5000](http://localhost:5000).
101
-
102
-
## Project Structure
103
-
104
-
-`index.js` - Main entry point, sets up Express, routes, and middleware
105
-
-`routes/` - Express route definitions for products, users, blogs
106
-
-`interface-adapters/` - Controllers, DB access, adapters, and middleware
107
-
-`application-business-rules/` - Use cases for products and users
108
-
-`enterprise-business-rules/` - Domain models and validation logic
109
-
-`public/` - Static HTML views (landing page, 404)
110
-
111
86
## API Endpoints
112
87
113
-
### Products
88
+
See the `routes/` directory for all endpoints. Example:
114
89
115
90
-`POST /products/` - Create a new product
116
91
-`GET /products/` - Get all products
117
-
-`GET /products/:productId` - Get a product by ID
118
-
-`PUT /products/:productId` - Update a product
119
-
-`DELETE /products/:productId` - Delete a product
120
-
-`POST /products/:productId/:userId/rating` - Rate a product
121
-
122
-
### Users & Auth
123
-
124
92
-`POST /users/register` - Register a new user
125
93
-`POST /users/login` - User login
126
-
-`GET /users/profile` - Get user profile (auth required)
127
-
128
-
### Blogs
129
-
130
94
-`GET /blogs/` - Get all blogs
131
-
-`POST /blogs/` - Create a new blog
132
95
133
-
> More endpoints and details can be found in the route files under `routes/`.
96
+
## API Documentation & Models (Swagger UI)
97
+
98
+
- Interactive API docs are available at `/api-docs` when the server is running.
99
+
- All endpoints are documented with request/response schemas using Swagger/OpenAPI.
100
+
-**Models:**
101
+
- Each resource (User, Product, Blog) has two main schemas:
102
+
-**Input Model** (e.g., `UserInput`, `ProductInput`, `BlogInput`): What the client sends when creating or updating a resource. Only includes fields the client can set (e.g., no `_id`, no server-generated fields).
103
+
-**Output Model** (e.g., `User`, `Product`, `Blog`): What the API returns. Includes all fields, including those generated by the server (e.g., `_id`, `role`, etc.).
104
+
- This separation improves security, clarity, and validation.
105
+
- You can view and try all models in the "Schemas" section of Swagger UI.
106
+
- check at http://localhost:5000/api-docs. /_ (:5000 depend on you chosen port) _/
134
107
135
108
## Testing
136
109
137
-
- Tests are written using [Jest](https://jestjs.io/) and [Supertest](https://github.com/visionmedia/supertest).
110
+
-**Unit tests** (Jest): Test business logic in isolation by injecting mocks for all dependencies. No real DB required.
111
+
-**Integration tests** (Supertest): Test the full stack, optionally with a real or in-memory DB.
138
112
- To run all tests:
139
113
```bash
140
114
yarn test
141
115
```
142
-
- Test files are located in the `tests/` directory.
116
+
- Test files are in the `tests/` directory.
143
117
144
118
## Linting & Formatting
145
119
@@ -160,7 +134,7 @@ The server will run at [http://localhost:5000](http://localhost:5000).
160
134
docker-compose up --build
161
135
```
162
136
- The app will be available at [http://localhost:5000](http://localhost:5000).
163
-
- The MongoDB service runs at `mongodb://localhost:27017/cleanarchdb`.
137
+
- The MongoDB service runs at `mongodb://mongo:27017/cleanarchdb` (inside Docker) or `localhost:27017` (locally).
164
138
- To stop and remove containers, networks, and volumes:
165
139
```bash
166
140
docker-compose down -v
@@ -169,18 +143,12 @@ The server will run at [http://localhost:5000](http://localhost:5000).
169
143
## CI/CD Workflow
170
144
171
145
- GitHub Actions workflow is set up in `.github/workflows/ci-cd.yml`.
172
-
- On push to `main`, the workflow:
173
-
- Installs dependencies
174
-
- Lints and formats code
175
-
- Runs tests
176
-
- Builds a Docker image
177
-
- Pushes the image to Docker Hub (update credentials and repo in workflow and GitHub secrets)
146
+
- On push to `main`, the workflow lints, tests, builds, and pushes a Docker image.
178
147
179
148
## Troubleshooting
180
149
181
-
- Common issues and solutions are documented in [troubleshooting.md](./troubleshooting.md).
182
-
- Please add new issues and solutions as you encounter them.
150
+
- See [troubleshooting.md](./troubleshooting.md) for common issues and solutions.
183
151
184
152
## License
185
153
186
-
This project is licensed under the ISC License. See the [LICENSE](LICENSE) file for details.
0 commit comments