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.
- Introduction
- Architecture Overview
- Features
- Getting Started
- Project Structure
- API Endpoints
- Testing
- Linting & Formatting
- Docker & Docker Compose
- CI/CD Workflow
- Troubleshooting
- License
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.
The project is organized into the following layers:
- Enterprise Business Rules: Core business logic and domain models (
enterprise-business-rules/
). - Application Business Rules: Use cases and application-specific logic (
application-business-rules/
). - Interface Adapters: Controllers, database access, adapters, and middlewares (
interface-adapters/
). - Frameworks & Drivers: Express.js, MongoDB, and other external libraries.
enterprise-business-rules/
entities/ # Domain models (User, Product, Rating, Blog)
validate-models/ # Validation logic for domain models
application-business-rules/
use-cases/ # Application use cases (products, user)
interface-adapters/
controllers/ # Route controllers for products, users
database-access/ # DB connection and data access logic
adapter/ # Adapters (e.g., request/response)
middlewares/ # Auth, logging, error handling
routes/ # Express route definitions
public/ # Static files and HTML views
- User registration and authentication (JWT)
- Product CRUD operations
- Blog and rating management
- Role-based access control (admin, blocked users)
- Input validation and error handling
- Modular, testable codebase
- Express.js
- Javascript
- MongoDB doker image
- Jest
- Mongo-client + Mongosh
- Node.js (v18+ recommended)
- MongoDB instance (local or cloud)
- Clone the repository:
git clone <repo-url> cd Clean-code-arch-REST-API
- Install dependencies:
yarn install
- Create a
.env
file in the root with your environment variables (see.env.example
if available):PORT=5000 MONGODB_URI=mongodb://localhost:27017/your-db JWT_SECRET=your_jwt_secret
- Start the server:
yarn dev # or yarn start
The server will run at http://localhost:5000.
index.js
- Main entry point, sets up Express, routes, and middlewareroutes/
- Express route definitions for products, users, blogsinterface-adapters/
- Controllers, DB access, adapters, and middlewareapplication-business-rules/
- Use cases for products and usersenterprise-business-rules/
- Domain models and validation logicpublic/
- Static HTML views (landing page, 404)
POST /products/
- Create a new productGET /products/
- Get all productsGET /products/:productId
- Get a product by IDPUT /products/:productId
- Update a productDELETE /products/:productId
- Delete a productPOST /products/:productId/:userId/rating
- Rate a product
POST /users/register
- Register a new userPOST /users/login
- User loginGET /users/profile
- Get user profile (auth required)
GET /blogs/
- Get all blogsPOST /blogs/
- Create a new blog
More endpoints and details can be found in the route files under
routes/
.
- Tests are written using Jest and Supertest.
- To run all tests:
yarn test
- Test files are located in the
tests/
directory.
- Lint your code:
yarn lint
- Format your code:
yarn format
- Prettier and ESLint are enforced on pre-push via Husky and lint-staged.
- Build and run the app with MongoDB using Docker Compose:
docker-compose up --build
- The app will be available at http://localhost:5000.
- The MongoDB service runs at
mongodb://localhost:27017/cleanarchdb
. - To stop and remove containers, networks, and volumes:
docker-compose down -v
- GitHub Actions workflow is set up in
.github/workflows/ci-cd.yml
. - On push to
main
, the workflow:- Installs dependencies
- Lints and formats code
- Runs tests
- Builds a Docker image
- Pushes the image to Docker Hub (update credentials and repo in workflow and GitHub secrets)
- Common issues and solutions are documented in troubleshooting.md.
- Please add new issues and solutions as you encounter them.
This project is licensed under the ISC License. See the LICENSE file for details.