Skip to content

Commit d5fd76c

Browse files
authored
Update README.md
1 parent 6a13b9e commit d5fd76c

File tree

1 file changed

+49
-81
lines changed

1 file changed

+49
-81
lines changed

README.md

Lines changed: 49 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,57 @@
1-
2-
# Clean code Architecture pattern applied to Digital Market Place API.
1+
# Clean code Architecture pattern applied to Node.js REST API Example
32

43
<div style="width:100%; text-align:center">
54
<img src="public/images/clean-code_arch.jpeg" width="600">
65
</div>
76

8-
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.
910
10-
## Table of Contents
11+
## Stack
1112

12-
- [Introduction](#introduction)
13-
- [Architecture Overview](#architecture-overview)
14-
- [Features](#features)
15-
- [Getting Started](#getting-started)
16-
- [Project Structure](#project-structure)
17-
- [API Endpoints](#api-endpoints)
18-
- [Testing](#testing)
19-
- [Linting & Formatting](#linting--formatting)
20-
- [Docker & Docker Compose](#docker--docker-compose)
21-
- [CI/CD Workflow](#cicd-workflow)
22-
- [Troubleshooting](#troubleshooting)
23-
- [License](#license)
13+
- **Node.js** (Express.js) for the REST API
14+
- **MongoDB** (MongoClient) for persistence
15+
- **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
2419

25-
## Introduction
20+
## Why Clean Architecture?
2621

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.
2826

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.
3029
31-
The project is organized into the following layers:
30+
## How Testing Works
3231

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.
37+
38+
## Project Structure
3739

3840
```
3941
enterprise-business-rules/
4042
entities/ # Domain models (User, Product, Rating, Blog)
4143
validate-models/ # Validation logic for domain models
4244
application-business-rules/
43-
use-cases/ # Application use cases (products, user)
45+
use-cases/ # Application use cases (products, user, blog)
4446
interface-adapters/
45-
controllers/ # Route controllers for products, users
47+
controllers/ # Route controllers for products, users, blogs
4648
database-access/ # DB connection and data access logic
4749
adapter/ # Adapters (e.g., request/response)
4850
middlewares/ # Auth, logging, error handling
4951
routes/ # Express route definitions
5052
public/ # Static files and HTML views
5153
```
5254

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-
6955
## Getting Started
7056

7157
### Prerequisites
@@ -84,10 +70,10 @@ public/ # Static files and HTML views
8470
```bash
8571
yarn install
8672
```
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:
8874
```env
8975
PORT=5000
90-
MONGODB_URI=mongodb://localhost:27017/your-db
76+
MONGO_URI=mongodb://localhost:27017/your-db
9177
JWT_SECRET=your_jwt_secret
9278
```
9379
4. Start the server:
@@ -97,49 +83,37 @@ public/ # Static files and HTML views
9783
yarn start
9884
```
9985

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-
11186
## API Endpoints
11287

113-
### Products
88+
See the `routes/` directory for all endpoints. Example:
11489

11590
- `POST /products/` - Create a new product
11691
- `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-
12492
- `POST /users/register` - Register a new user
12593
- `POST /users/login` - User login
126-
- `GET /users/profile` - Get user profile (auth required)
127-
128-
### Blogs
129-
13094
- `GET /blogs/` - Get all blogs
131-
- `POST /blogs/` - Create a new blog
13295

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) _/
134107

135108
## Testing
136109

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.
138112
- To run all tests:
139113
```bash
140114
yarn test
141115
```
142-
- Test files are located in the `tests/` directory.
116+
- Test files are in the `tests/` directory.
143117

144118
## Linting & Formatting
145119

@@ -160,7 +134,7 @@ The server will run at [http://localhost:5000](http://localhost:5000).
160134
docker-compose up --build
161135
```
162136
- 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).
164138
- To stop and remove containers, networks, and volumes:
165139
```bash
166140
docker-compose down -v
@@ -169,18 +143,12 @@ The server will run at [http://localhost:5000](http://localhost:5000).
169143
## CI/CD Workflow
170144

171145
- 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.
178147

179148
## Troubleshooting
180149

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.
183151

184152
## License
185153

186-
This project is licensed under the ISC License. See the [LICENSE](LICENSE) file for details.
154+
ISC License. See [LICENSE](LICENSE).

0 commit comments

Comments
 (0)