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
> 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.
10
10
11
11
## Stack
12
+
12
13
-**Node.js** (Express.js) for the REST API
13
14
-**MongoDB** (MongoClient) for persistence
14
15
-**Jest** & **Supertest** for unit and integration testing
@@ -17,6 +18,7 @@
17
18
-**GitHub Actions** for CI/CD
18
19
19
20
## Why Clean Architecture?
21
+
20
22
-**Separation of Concerns:** Each layer has a single responsibility and is independent from others.
21
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.
22
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.
@@ -26,13 +28,15 @@
26
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.
27
29
28
30
## How Testing Works
31
+
29
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.
30
33
-**Integration tests** can use a real or in-memory database, but the architecture allows you to swap these easily.
31
34
-**Example:**
32
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.
33
36
- Lower layers (domain, use cases) never import or reference Express, MongoDB, or any framework code.
@@ -51,10 +55,12 @@ public/ # Static files and HTML views
51
55
## Getting Started
52
56
53
57
### Prerequisites
58
+
54
59
- Node.js (v18+ recommended)
55
60
- MongoDB instance (local or cloud)
56
61
57
62
### Installation
63
+
58
64
1. Clone the repository:
59
65
```bash
60
66
git clone <repo-url>
@@ -78,14 +84,17 @@ public/ # Static files and HTML views
78
84
```
79
85
80
86
## API Endpoints
87
+
81
88
See the `routes/` directory for all endpoints. Example:
89
+
82
90
-`POST /products/` - Create a new product
83
91
-`GET /products/` - Get all products
84
92
-`POST /users/register` - Register a new user
85
93
-`POST /users/login` - User login
86
94
-`GET /blogs/` - Get all blogs
87
95
88
96
## API Documentation & Models (Swagger UI)
97
+
89
98
- Interactive API docs are available at `/api-docs` when the server is running.
90
99
- All endpoints are documented with request/response schemas using Swagger/OpenAPI.
91
100
-**Models:**
@@ -94,11 +103,10 @@ See the `routes/` directory for all endpoints. Example:
94
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.).
95
104
- This separation improves security, clarity, and validation.
96
105
- You can view and try all models in the "Schemas" section of Swagger UI.
97
-
- check at http://localhost:5000/api-docs. /* (:5000 depend on you chosen port) */
98
-
99
-
106
+
- check at http://localhost:5000/api-docs. /_ (:5000 depend on you chosen port) _/
100
107
101
108
## Testing
109
+
102
110
-**Unit tests** (Jest): Test business logic in isolation by injecting mocks for all dependencies. No real DB required.
103
111
-**Integration tests** (Supertest): Test the full stack, optionally with a real or in-memory DB.
104
112
- To run all tests:
@@ -108,6 +116,7 @@ See the `routes/` directory for all endpoints. Example:
108
116
- Test files are in the `tests/` directory.
109
117
110
118
## Linting & Formatting
119
+
111
120
- Lint your code:
112
121
```bash
113
122
yarn lint
@@ -119,6 +128,7 @@ See the `routes/` directory for all endpoints. Example:
119
128
- Prettier and ESLint are enforced on pre-push via Husky and lint-staged.
120
129
121
130
## Docker & Docker Compose
131
+
122
132
- Build and run the app with MongoDB using Docker Compose:
123
133
```bash
124
134
docker-compose up --build
@@ -131,11 +141,14 @@ See the `routes/` directory for all endpoints. Example:
131
141
```
132
142
133
143
## CI/CD Workflow
144
+
134
145
- GitHub Actions workflow is set up in `.github/workflows/ci-cd.yml`.
135
146
- On push to `main`, the workflow lints, tests, builds, and pushes a Docker image.
136
147
137
148
## Troubleshooting
149
+
138
150
- See [troubleshooting.md](./troubleshooting.md) for common issues and solutions.
0 commit comments