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.
> 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
15
+
-**ESLint** & **Prettier** for linting and formatting
16
+
-**Docker** & **Docker Compose** for containerization
17
+
-**GitHub Actions** for CI/CD
21
18
22
-
## Introduction
19
+
## Why Clean Architecture?
20
+
-**Separation of Concerns:** Each layer has a single responsibility and is independent from others.
21
+
-**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.
22
+
-**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.
23
+
-**Security & Flexibility:** Infrastructure (DB, frameworks) can be swapped without touching business logic.
23
24
24
-
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.
25
-
26
-
## Architecture Overview
27
-
28
-
The project is organized into the following layers:
29
-
30
-
-**Enterprise Business Rules**: Core business logic and domain models (`enterprise-business-rules/`).
31
-
-**Application Business Rules**: Use cases and application-specific logic (`application-business-rules/`).
32
-
-**Interface Adapters**: Controllers, database access, adapters, and middlewares (`interface-adapters/`).
33
-
-**Frameworks & Drivers**: Express.js, MongoDB, and other external libraries.
25
+
## How Testing Works
26
+
-**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.
27
+
-**Integration tests** can use a real or in-memory database, but the architecture allows you to swap these easily.
28
+
-**Example:**
29
+
- 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.
30
+
- Lower layers (domain, use cases) never import or reference Express, MongoDB, or any framework code.
validate-models/ # Validation logic for domain models
39
37
application-business-rules/
40
-
use-cases/ # Application use cases (products, user)
38
+
use-cases/ # Application use cases (products, user, blog)
41
39
interface-adapters/
42
-
controllers/ # Route controllers for products, users
40
+
controllers/ # Route controllers for products, users, blogs
43
41
database-access/ # DB connection and data access logic
44
42
adapter/ # Adapters (e.g., request/response)
45
43
middlewares/ # Auth, logging, error handling
46
44
routes/ # Express route definitions
47
45
public/ # Static files and HTML views
48
46
```
49
47
50
-
## Features
51
-
52
-
- User registration and authentication (JWT)
53
-
- Product CRUD operations
54
-
- Blog and rating management
55
-
- Role-based access control (admin, blocked users)
56
-
- Input validation and error handling
57
-
- Modular, testable codebase
58
-
59
48
## Getting Started
60
49
61
50
### Prerequisites
62
-
63
51
- Node.js (v18+ recommended)
64
52
- MongoDB instance (local or cloud)
65
53
66
54
### Installation
67
-
68
55
1. Clone the repository:
69
56
```bash
70
57
git clone <repo-url>
@@ -74,10 +61,10 @@ public/ # Static files and HTML views
74
61
```bash
75
62
yarn install
76
63
```
77
-
3. Create a `.env` file in the root with your environment variables (see `.env.example` if available):
64
+
3. Create a `.env` file in the root with your environment variables:
78
65
```env
79
66
PORT=5000
80
-
MONGODB_URI=mongodb://localhost:27017/your-db
67
+
MONGO_URI=mongodb://localhost:27017/your-db
81
68
JWT_SECRET=your_jwt_secret
82
69
```
83
70
4. Start the server:
@@ -87,52 +74,34 @@ public/ # Static files and HTML views
87
74
yarn start
88
75
```
89
76
90
-
The server will run at [http://localhost:5000](http://localhost:5000).
91
-
92
-
## Project Structure
93
-
94
-
-`index.js` - Main entry point, sets up Express, routes, and middleware
95
-
-`routes/` - Express route definitions for products, users, blogs
96
-
-`interface-adapters/` - Controllers, DB access, adapters, and middleware
97
-
-`application-business-rules/` - Use cases for products and users
98
-
-`enterprise-business-rules/` - Domain models and validation logic
99
-
-`public/` - Static HTML views (landing page, 404)
100
-
101
77
## API Endpoints
102
-
103
-
### Products
104
-
78
+
See the `routes/` directory for all endpoints. Example:
105
79
-`POST /products/` - Create a new product
106
80
-`GET /products/` - Get all products
107
-
-`GET /products/:productId` - Get a product by ID
108
-
-`PUT /products/:productId` - Update a product
109
-
-`DELETE /products/:productId` - Delete a product
110
-
-`POST /products/:productId/:userId/rating` - Rate a product
111
-
112
-
### Users & Auth
113
-
114
81
-`POST /users/register` - Register a new user
115
82
-`POST /users/login` - User login
116
-
-`GET /users/profile` - Get user profile (auth required)
117
-
118
-
### Blogs
119
-
120
83
-`GET /blogs/` - Get all blogs
121
-
-`POST /blogs/` - Create a new blog
122
84
123
-
> More endpoints and details can be found in the route files under `routes/`.
85
+
## API Documentation & Models (Swagger UI)
86
+
- Interactive API docs are available at `/api-docs` when the server is running.
87
+
- All endpoints are documented with request/response schemas using Swagger/OpenAPI.
88
+
-**Models:**
89
+
- Each resource (User, Product, Blog) has two main schemas:
90
+
-**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).
91
+
-**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.).
92
+
- This separation improves security, clarity, and validation.
93
+
- You can view and try all models in the "Schemas" section of Swagger UI.
124
94
125
95
## Testing
126
-
127
-
-Tests are written using [Jest](https://jestjs.io/) and [Supertest](https://github.com/visionmedia/supertest).
96
+
-**Unit tests** (Jest): Test business logic in isolation by injecting mocks for all dependencies. No real DB required.
97
+
-**Integration tests** (Supertest): Test the full stack, optionally with a real or in-memory DB.
128
98
- To run all tests:
129
99
```bash
130
100
yarn test
131
101
```
132
-
- Test files are located in the `tests/` directory.
102
+
- Test files are in the `tests/` directory.
133
103
134
104
## Linting & Formatting
135
-
136
105
- Lint your code:
137
106
```bash
138
107
yarn lint
@@ -144,33 +113,23 @@ The server will run at [http://localhost:5000](http://localhost:5000).
144
113
- Prettier and ESLint are enforced on pre-push via Husky and lint-staged.
145
114
146
115
## Docker & Docker Compose
147
-
148
116
- Build and run the app with MongoDB using Docker Compose:
149
117
```bash
150
118
docker-compose up --build
151
119
```
152
120
- The app will be available at [http://localhost:5000](http://localhost:5000).
153
-
- The MongoDB service runs at `mongodb://localhost:27017/cleanarchdb`.
121
+
- The MongoDB service runs at `mongodb://mongo:27017/cleanarchdb` (inside Docker) or `localhost:27017` (locally).
154
122
- To stop and remove containers, networks, and volumes:
155
123
```bash
156
124
docker-compose down -v
157
125
```
158
126
159
127
## CI/CD Workflow
160
-
161
128
- GitHub Actions workflow is set up in `.github/workflows/ci-cd.yml`.
162
-
- On push to `main`, the workflow:
163
-
- Installs dependencies
164
-
- Lints and formats code
165
-
- Runs tests
166
-
- Builds a Docker image
167
-
- Pushes the image to Docker Hub (update credentials and repo in workflow and GitHub secrets)
129
+
- On push to `main`, the workflow lints, tests, builds, and pushes a Docker image.
168
130
169
131
## Troubleshooting
170
-
171
-
- Common issues and solutions are documented in [troubleshooting.md](./troubleshooting.md).
172
-
- Please add new issues and solutions as you encounter them.
132
+
- See [troubleshooting.md](./troubleshooting.md) for common issues and solutions.
173
133
174
134
## License
175
-
176
-
This project is licensed under the ISC License. See the [LICENSE](LICENSE) file for details.
0 commit comments