Skip to content

Commit 6e91a53

Browse files
committed
docs: update README and troubleshooting guide for flexibility and recent changes
1 parent 83e9491 commit 6e91a53

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
21
# Clean Architecture 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

87
**Objective:**
8+
99
> 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.
1010
1111
## Stack
12+
1213
- **Node.js** (Express.js) for the REST API
1314
- **MongoDB** (MongoClient) for persistence
1415
- **Jest** & **Supertest** for unit and integration testing
@@ -17,6 +18,7 @@
1718
- **GitHub Actions** for CI/CD
1819

1920
## Why Clean Architecture?
21+
2022
- **Separation of Concerns:** Each layer has a single responsibility and is independent from others.
2123
- **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.
2224
- **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 @@
2628
> 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.
2729
2830
## How Testing Works
31+
2932
- **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.
3033
- **Integration tests** can use a real or in-memory database, but the architecture allows you to swap these easily.
3134
- **Example:**
3235
- 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.
3336
- Lower layers (domain, use cases) never import or reference Express, MongoDB, or any framework code.
3437

3538
## Project Structure
39+
3640
```
3741
enterprise-business-rules/
3842
entities/ # Domain models (User, Product, Rating, Blog)
@@ -51,10 +55,12 @@ public/ # Static files and HTML views
5155
## Getting Started
5256

5357
### Prerequisites
58+
5459
- Node.js (v18+ recommended)
5560
- MongoDB instance (local or cloud)
5661

5762
### Installation
63+
5864
1. Clone the repository:
5965
```bash
6066
git clone <repo-url>
@@ -78,14 +84,17 @@ public/ # Static files and HTML views
7884
```
7985

8086
## API Endpoints
87+
8188
See the `routes/` directory for all endpoints. Example:
89+
8290
- `POST /products/` - Create a new product
8391
- `GET /products/` - Get all products
8492
- `POST /users/register` - Register a new user
8593
- `POST /users/login` - User login
8694
- `GET /blogs/` - Get all blogs
8795

8896
## API Documentation & Models (Swagger UI)
97+
8998
- Interactive API docs are available at `/api-docs` when the server is running.
9099
- All endpoints are documented with request/response schemas using Swagger/OpenAPI.
91100
- **Models:**
@@ -94,11 +103,10 @@ See the `routes/` directory for all endpoints. Example:
94103
- **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.).
95104
- This separation improves security, clarity, and validation.
96105
- 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) _/
100107

101108
## Testing
109+
102110
- **Unit tests** (Jest): Test business logic in isolation by injecting mocks for all dependencies. No real DB required.
103111
- **Integration tests** (Supertest): Test the full stack, optionally with a real or in-memory DB.
104112
- To run all tests:
@@ -108,6 +116,7 @@ See the `routes/` directory for all endpoints. Example:
108116
- Test files are in the `tests/` directory.
109117

110118
## Linting & Formatting
119+
111120
- Lint your code:
112121
```bash
113122
yarn lint
@@ -119,6 +128,7 @@ See the `routes/` directory for all endpoints. Example:
119128
- Prettier and ESLint are enforced on pre-push via Husky and lint-staged.
120129

121130
## Docker & Docker Compose
131+
122132
- Build and run the app with MongoDB using Docker Compose:
123133
```bash
124134
docker-compose up --build
@@ -131,11 +141,14 @@ See the `routes/` directory for all endpoints. Example:
131141
```
132142

133143
## CI/CD Workflow
144+
134145
- GitHub Actions workflow is set up in `.github/workflows/ci-cd.yml`.
135146
- On push to `main`, the workflow lints, tests, builds, and pushes a Docker image.
136147

137148
## Troubleshooting
149+
138150
- See [troubleshooting.md](./troubleshooting.md) for common issues and solutions.
139151

140152
## License
153+
141154
ISC License. See [LICENSE](LICENSE).

troubleshooting.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
## 0. Express Downgrade & Docker Restart for Compatibility
66

77
**Symptom:**
8+
89
- Swagger UI or other middleware fails with errors related to `path-to-regexp` or route registration after upgrading Express (e.g., Express v5 beta).
910
- Docker Compose or MongoDB connection errors after system or Docker Desktop restart.
1011

1112
**Solution:**
13+
1214
- Downgrade Express to v4 (e.g., `npm install express@4` or `yarn add express@4`).
1315
- Stop Docker Desktop completely (kill all Docker processes if needed), then restart Docker Desktop and wait for it to be fully running.
1416
- Run `docker-compose up -d` to restart all services.
@@ -19,15 +21,18 @@
1921
## 0.1. Swagger UI Not Working
2022

2123
**Symptom:**
24+
2225
- Navigating to `/api-docs` returns a 404, blank page, or error.
2326
- Swagger UI does not load or shows a path-to-regexp or route registration error.
2427

2528
**Possible Causes:**
29+
2630
- Swagger UI route is registered after a catch-all or error handler route in Express.
2731
- Express version incompatibility (v5 beta is not supported by swagger-ui-express).
2832
- Incorrect Swagger JSDoc configuration or missing comments.
2933

3034
**Next Steps:**
35+
3136
- Ensure `app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec))` is registered before any catch-all or error handler middleware.
3237
- Confirm Express is v4, not v5.
3338
- Check for valid Swagger JSDoc comments above all route definitions.

0 commit comments

Comments
 (0)