# aayushcodesfx/e-commerce-backend architecture

> Architecture diagram of the GitHub repository aayushcodesfx/e-commerce-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aayushcodesfx/e-commerce-backend
- Repository: https://github.com/aayushcodesfx/e-commerce-backend

## Overview

##### Project type and purpose
- This is a monolithic, REST-ful backend server for an e-commerce application.
- Built with Node.js, Express.js, TypeScript, and MongoDB via Mongoose.
- It provides user authentication, product/cart/wishlist management, order processing, file uploads, and email notifications.

##### File structure
- Top-level:
- src/ – all TS source code (controllers, routes, models, middleware, utils, config, types).
- build/ – compiled JS output (mirrors src structure).
- uploads/ – static file storage (images).
- tsconfig.json, package.json – build/config.
- src/ breakdown:
- config/database.config.ts – MongoDB connection.
- controllers/*.controller.ts – business logic per resource.
- routes/*.routes.ts – Express router definitions.
- models/*.model.ts – Mongoose schemas.
- middleware/*.middleware.ts – auth & error‐handling layers.
- utils/*.utils.ts – helper functions (JWT, bcrypt, email, pagination, file deletion).
- @types/ – global and extension type definitions.

##### README for additional insights
- Lists all API endpoints and features: auth, products, cart, orders.
- Mentions external services: MongoDB Atlas, JWT, bcrypt, multer for uploads, Nodemailer for emails.
- Emphasizes best practices: environment variables, modular structure, global error handling.

a. Components:
1. Client (Web or Mobile)
2. Express API Server
- Routes Layer
- Middleware Layer (Authentication, Error Handler)
- Controller Layer
- Utility/Service Layer (JWT, bcrypt, email, pagination, file utils)
3. Data Layer (MongoDB via Mongoose models)
4. External Services
- SMTP Email Provider (Nodemailer)
- File Storage (local filesystem under uploads/)
b. Relationships & data‐flow:
- Client → HTTP Requests → Express Routes
- Routes → Authentication Middleware → Controllers
- Controllers ↔ Models (CRUD calls to MongoDB)
- Controllers → Email Utility → SMTP Service
- Controllers → File Utility → uploads/ folder
- Controllers → Response → Client
c. Architectural patterns/design principles:
- Layered MVC-style separation (Routes → Controllers → Models)
- Middleware pipeline for cross-cutting concerns (auth, errors)
- Utility modules for shared logic (single-responsibility)
- Environment-driven configuration (12-factor app)
d. Key technologies:
- Node.js + Express.js – HTTP server & routing
- TypeScript – type safety across layers
- Mongoose – ODM for MongoDB
- JWT & bcrypt – stateless auth and password hashing
- Multer – multipart/form uploads
- Nodemailer – email notifications

##### Detailed breakdown to capture essential elements
- Entry point: index.ts → Express app setup → loads config (database.config).
- Middleware chain: authentication middleware → route handlers.
- Route grouping: /api/auth, /api/products, /api/cart, /api/orders, /api/wishlist, /api/reviews.
- Services: JWT utils (token issue/verify), bcrypt utils (hash/compare), email utils (order confirmation), file utils (multer config, deletion).
- Data models: User, Product, Category, Cart, Order, Review, Wishlist.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Client (Web/Mobile)"]:::client

    subgraph "API Server"
        entry["Entry Point\nindex.ts"]:::backend
        config["Database Config\nconfig/database.config.ts"]:::backend

        subgraph "Routes Layer"
            userRoutes["User Routes\nuser.routes.ts"]:::backend
            productRoutes["Product Routes\nproduct.routes.ts"]:::backend
            cartRoutes["Cart Routes\ncart.routes.ts"]:::backend
            wishlistRoutes["Wishlist Routes\nwishlist.routes.ts"]:::backend
            orderRoutes["Order Routes\norder.routes.ts"]:::backend
            reviewRoutes["Review Routes\nreview.routes.ts"]:::backend
        end

        subgraph "Middleware Layer"
            authMw["Auth Middleware\nauthentication.middleware.ts"]:::backend
            errorMw["Error Handler\nerrorhandler.middleware.ts"]:::backend
        end

        subgraph "Controller Layer"
            userCtrl["User Controller\nuser.controller.ts"]:::backend
            productCtrl["Product Controller\nproduct.controller.ts"]:::backend
            cartCtrl["Cart Controller\ncart.controller.ts"]:::backend
            wishlistCtrl["Wishlist Controller\nwishlist.controller.ts"]:::backend
            orderCtrl["Order Controller\norder.controller.ts"]:::backend
            reviewCtrl["Review Controller\nreview.controller.ts"]:::backend
        end

        subgraph "Service/Utils"
            jwtUtil["JWT Utilities\njwt.utils.ts"]:::utils
            bcryptUtil["Bcrypt Utilities\nbcrypt.utils.ts"]:::utils
            emailGen["Send Email\nsendemail.utils.ts"]:::utils
            orderEmail["Order Confirmation\norderconfirmationEmail.utils.ts"]:::utils
            fileUtil["File Utilities\ndeleteFiles.utils.ts"]:::utils
            pagination["Pagination Utils\npagination.utils.ts"]:::utils
            asyncWrap["Async Handler\nasyncHandler.utils.ts"]:::utils
        end

        subgraph "Models"
            userModel["User Model\nusers.model.ts"]:::database
            productModel["Product Model\nproduct.model.ts"]:::database
            categoryModel["Category Model\ncategory.model.ts"]:::database
            cartModel["Cart Model\ncart.model.ts"]:::database
            orderModel["Order Model\norder.model.ts"]:::database
            reviewModel["Review Model\nreview.model.ts"]:::database
        end
    end

    db["MongoDB Atlas"]:::database
    uploads["Uploads Folder"]:::external
    smtp["SMTP Service"]:::external

    Client -->|HTTP/JSON| entry
    entry --> config
    entry --> userRoutes
    entry --> productRoutes
    entry --> cartRoutes
    entry --> wishlistRoutes
    entry --> orderRoutes
    entry --> reviewRoutes

    userRoutes --> authMw
    productRoutes --> authMw
    cartRoutes --> authMw
    wishlistRoutes --> authMw
    orderRoutes --> authMw
    reviewRoutes --> authMw

    authMw --> userCtrl
    authMw --> productCtrl
    authMw --> cartCtrl
    authMw --> wishlistCtrl
    authMw --> orderCtrl
    authMw --> reviewCtrl

    userCtrl --> userModel
    productCtrl --> productModel
    productCtrl --> categoryModel
    cartCtrl --> cartModel
    orderCtrl --> orderModel
    reviewCtrl --> reviewModel

    userModel --> db
    productModel --> db
    categoryModel --> db
    cartModel --> db
    orderModel --> db
    reviewModel --> db

    userCtrl --> jwtUtil
    userCtrl --> bcryptUtil
    orderCtrl --> emailGen
    orderCtrl --> orderEmail
    productCtrl --> fileUtil
    cartCtrl --> pagination
    wishlistCtrl --> pagination
    reviewCtrl --> pagination

    fileUtil --> uploads
    emailGen --> smtp
    orderEmail --> smtp

    userCtrl -->|Response| Client
    productCtrl -->|Response| Client
    cartCtrl -->|Response| Client
    wishlistCtrl -->|Response| Client
    orderCtrl -->|Response| Client
    reviewCtrl -->|Response| Client

    click entry "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/index.ts"
    click config "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/config/database.config.ts"
    click userRoutes "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/routes/user.routes.ts"
    click productRoutes "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/routes/product.routes.ts"
    click cartRoutes "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/routes/cart.routes.ts"
    click wishlistRoutes "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/routes/wishlist.routes.ts"
    click orderRoutes "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/routes/order.routes.ts"
    click reviewRoutes "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/routes/review.routes.ts"
    click authMw "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/middleware/authentication.middleware.ts"
    click errorMw "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/middleware/errorhandler.middleware.ts"
    click userCtrl "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/controllers/user.controller.ts"
    click productCtrl "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/controllers/product.controller.ts"
    click cartCtrl "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/controllers/cart.controller.ts"
    click wishlistCtrl "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/controllers/wishlist.controller.ts"
    click orderCtrl "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/controllers/order.controller.ts"
    click reviewCtrl "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/controllers/review.controller.ts"
    click userModel "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/models/users.model.ts"
    click productModel "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/models/product.model.ts"
    click categoryModel "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/models/category.model.ts"
    click cartModel "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/models/cart.model.ts"
    click orderModel "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/models/order.model.ts"
    click reviewModel "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/models/review.model.ts"
    click jwtUtil "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/utils/jwt.utils.ts"
    click bcryptUtil "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/utils/bcrypt.utils.ts"
    click emailGen "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/utils/sendemail.utils.ts"
    click orderEmail "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/utils/orderconfirmationEmail.utils.ts"
    click fileUtil "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/utils/deleteFIles.utils.ts"
    click pagination "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/utils/pagination.utils.ts"
    click asyncWrap "https://github.com/aayushcodesfx/e-commerce-backend/blob/main/src/utils/asyncHandler.utils.ts"

    classDef client fill:#dddddd,stroke:#888,stroke-width:1px
    classDef backend fill:#bbdefb,stroke:#1e88e5,stroke-width:1px
    classDef database fill:#c8e6c9,stroke:#388e3c,stroke-width:1px
    classDef external fill:#ffe0b2,stroke:#f57c00,stroke-width:1px
    classDef utils fill:#e1bee7,stroke:#8e24aa,stroke-width:1px
```
