# body661/shoppio-api architecture

> Architecture diagram of the GitHub repository body661/shoppio-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/body661/shoppio-api
- Repository: https://github.com/body661/shoppio-api

## Overview

##### Project type and purpose
- This is a monolithic, backend-only e-commerce API built with Node.js and Express. It provides REST endpoints for users, products, orders, carts, coupons, reviews, etc., backed by MongoDB, with integrations for file uploads (images), payments (Stripe), email (Nodemailer/Handlebars), security middleware (helmet, HPP, XSS-clean), rate limiting, and JWT authentication.

##### File structure
- Top level:
- server.mjs – application entry point
- config/db.mjs – database initialization
- .env template
- Routes/ – HTTP route definitions (each resource in its own file)
- controllers/ – business-logic handlers invoked by routes
- middlewares/ – cross-cutting concerns (error handling, validation, image upload/processing)
- models/ – Mongoose schemas for each resource
- templates/ – email templates for welcome, password reset, etc.
- utils/ – shared helpers (JWT signing, API errors, pagination, sanitization, email sending)

##### README for additional insights
- Technology stack: Node.js, Express, MongoDB/Mongoose, AWS (for file storage), Stripe, JWT, CORS, rate limiting, data-sanitization tools, email via Nodemailer + Handlebars.
- Feature list confirms core components: Authentication/Authorization, Image Upload & Processing, Email Notification, Payment Processing, Security hardening.

##### Main components & interactions
a. Components
1. Client / Frontend Application
3. Express.js App (one or more instances)
– Routes Layer
– Middleware Layer (Validation, Auth, Rate-Limit, CORS, Security)
– Controllers / Service Layer
– Utils (JWT, Email, Error Handling, Sanitization)
4. MongoDB Atlas (or self-hosted)
5. AWS S3 (for image storage)
6. Stripe Payment Service
7. SMTP / Email Service (e.g., AWS SES)
b. Relationships & Data Flows
- Client → HTTP(S) → Load Balancer → Express Routes
- Route → Validator Middleware → Auth Middleware
- Auth Middleware → Controller
- Controller → Model (Mongoose ORM) → MongoDB
- Controller → Utils.sendEmail → Email Service
- Controller → Stripe SDK → Stripe API
- Image Upload Route → Multer → Sharp → AWS S3
c. Architectural Patterns & Principles
– Layered Architecture: Presentation (routes), Application (controllers), Data (models)
– Separation of Concerns: validation, business logic, file processing, error handling in dedicated middleware/modules
– Externalized configuration via environment variables
– Stateless JWT for auth, enabling horizontal scaling
d. Key Technologies
– Express.js: routing & middleware
– Mongoose: MongoDB ODM
– Multer & Sharp: file upload & processing
– AWS SDK: S3 storage (images)
– Stripe Node.js SDK: payments
– Nodemailer + Handlebars: templated emails
– Helmet, HPP, XSS-clean, cors, express-rate-limit: security

##### Detailed element list
– Client (browser/mobile)
– [Optional] Load Balancer / API Gateway
– Express.js Instances
- Routes: `/api/auth`, `/api/users`, `/api/products`, etc.
- Middleware Chain: validatorMiddleware → authMiddleware → errorMiddleware
- Controllers: AuthController, ProductController, OrderController, etc.
- Utils: signJWT, sendEmail, apiError, apiFeatures
– MongoDB (collections for users, products, orders, carts, reviews, etc.)
– AWS S3 bucket for image storage
– Stripe API for payment processing
– SMTP/SES for email notifications
– Config Service: environment variables loader

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Client"]:::client
    LB["Load Balancer / API Gateway"]:::gateway
    SEP["server.mjs"]:::entry
    click SEP "https://github.com/body661/shoppio-api/blob/main/server.mjs"
    DBInit["config/db.mjs"]:::config
    click DBInit "https://github.com/body661/shoppio-api/blob/main/config/db.mjs"
    subgraph "Routes Layer"
        UR["User Routes"]:::routes
        click UR "https://github.com/body661/shoppio-api/blob/main/Routes/user.mjs"
        AR["Auth Routes"]:::routes
        click AR "https://github.com/body661/shoppio-api/blob/main/Routes/auth.mjs"
        PR["Product Routes"]:::routes
        click PR "https://github.com/body661/shoppio-api/blob/main/Routes/product.mjs"
        OR["Order Routes"]:::routes
        click OR "https://github.com/body661/shoppio-api/blob/main/Routes/order.mjs"
        CR["Cart Routes"]:::routes
        click CR "https://github.com/body661/shoppio-api/blob/main/Routes/cart.mjs"
        CPR["Coupon Routes"]:::routes
        click CPR "https://github.com/body661/shoppio-api/blob/main/Routes/coupon.mjs"
        RR["Review Routes"]:::routes
        click RR "https://github.com/body661/shoppio-api/blob/main/Routes/review.mjs"
        BR["Brand Routes"]:::routes
        click BR "https://github.com/body661/shoppio-api/blob/main/Routes/brand.mjs"
        CatR["Category Routes"]:::routes
        click CatR "https://github.com/body661/shoppio-api/blob/main/Routes/category.mjs"
        SubCatR["Subcategory Routes"]:::routes
        click SubCatR "https://github.com/body661/shoppio-api/blob/main/Routes/subcategory.mjs"
        AdrR["Address Routes"]:::routes
        click AdrR "https://github.com/body661/shoppio-api/blob/main/Routes/address.mjs"
        WishR["Wishlist Routes"]:::routes
        click WishR "https://github.com/body661/shoppio-api/blob/main/Routes/wishlist.mjs"
        ValR["Route Validators"]:::routes
        click ValR "https://github.com/body661/shoppio-api/blob/main/Routes/validators/*.mjs"
    end
    subgraph "Middleware Layer"
        ErrM["Error Handling"]:::middleware
        click ErrM "https://github.com/body661/shoppio-api/blob/main/middlewares/errorMiddleware.mjs"
        ValM["Request Validation"]:::middleware
        click ValM "https://github.com/body661/shoppio-api/blob/main/middlewares/validatorMiddleware.mjs"
        UpM["Image Upload"]:::middleware
        click UpM "https://github.com/body661/shoppio-api/blob/main/middlewares/imageUploadMiddleware.mjs"
        ProcM["Image Processing"]:::middleware
        click ProcM "https://github.com/body661/shoppio-api/blob/main/middlewares/imageProcessingMiddleware.mjs"
        ClnM["Image Cleanup"]:::middleware
        click ClnM "https://github.com/body661/shoppio-api/blob/main/middlewares/deleteImageMiddleware.mjs"
        AuthM["Auth Middleware"]:::middleware
    end
    subgraph "Controllers"
        AuthC["Auth Controller"]:::controller
        click AuthC "https://github.com/body661/shoppio-api/blob/main/controllers/authController.mjs"
        UserC["User Controller"]:::controller
        click UserC "https://github.com/body661/shoppio-api/blob/main/controllers/userControllers.mjs"
        ProdC["Product Controller"]:::controller
        click ProdC "https://github.com/body661/shoppio-api/blob/main/controllers/productControllers.mjs"
        OrdC["Order Controller"]:::controller
        click OrdC "https://github.com/body661/shoppio-api/blob/main/controllers/orderController.mjs"
        CartC["Cart Controller"]:::controller
        click CartC "https://github.com/body661/shoppio-api/blob/main/controllers/cartController.mjs"
        CoupC["Coupon Controller"]:::controller
        click CoupC "https://github.com/body661/shoppio-api/blob/main/controllers/couponController.mjs"
        RevC["Review Controller"]:::controller
        click RevC "https://github.com/body661/shoppio-api/blob/main/controllers/reviewControllers.mjs"
        BrandC["Brand Controller"]:::controller
        click BrandC "https://github.com/body661/shoppio-api/blob/main/controllers/brandControllers.mjs"
        CatC["Category Controller"]:::controller
        click CatC "https://github.com/body661/shoppio-api/blob/main/controllers/categoryControllers.mjs"
        SubCatC["Subcategory Controller"]:::controller
        click SubCatC "https://github.com/body661/shoppio-api/blob/main/controllers/subcategoryControllers.mjs"
        AdrC["Address Controller"]:::controller
        click AdrC "https://github.com/body661/shoppio-api/blob/main/controllers/addressController.mjs"
        WishC["Wishlist Controller"]:::controller
        click WishC "https://github.com/body661/shoppio-api/blob/main/controllers/wishlistCntroller.mjs"
    end
    subgraph "Models (Mongoose)"
        UM["User Model"]:::model
        click UM "https://github.com/body661/shoppio-api/blob/main/models/userModel.mjs"
        PM["Product Model"]:::model
        click PM "https://github.com/body661/shoppio-api/blob/main/models/productModel.mjs"
        OM["Order Model"]:::model
        click OM "https://github.com/body661/shoppio-api/blob/main/models/orderModel.mjs"
        CaM["Cart Model"]:::model
        click CaM "https://github.com/body661/shoppio-api/blob/main/models/cartModel.mjs"
        CoM["Coupon Model"]:::model
        click CoM "https://github.com/body661/shoppio-api/blob/main/models/couponModel.mjs"
        ReM["Review Model"]:::model
        click ReM "https://github.com/body661/shoppio-api/blob/main/models/reviewModel.mjs"
        BM["Brand Model"]:::model
        click BM "https://github.com/body661/shoppio-api/blob/main/models/brandModel.mjs"
        CM["Category Model"]:::model
        click CM "https://github.com/body661/shoppio-api/blob/main/models/categoryModel.mjs"
        SCM["Subcategory Model"]:::model
        click SCM "https://github.com/body661/shoppio-api/blob/main/models/subcategoryModel.mjs"
        AM["Address Model"]:::model
        click AM "https://github.com/body661/shoppio-api/blob/main/models/categoryModel.mjs"
    end
    subgraph "Utils"
        JWT["signJWT"]:::util
        click JWT "https://github.com/body661/shoppio-api/blob/main/utils/signJWT.mjs"
        EmailU["sendEmail"]:::util
        click EmailU "https://github.com/body661/shoppio-api/blob/main/utils/sendEmail.mjs"
        ErrU["apiError"]:::util
        click ErrU "https://github.com/body661/shoppio-api/blob/main/utils/apiError.mjs"
        AF["apiFeatures"]:::util
        click AF "https://github.com/body661/shoppio-api/blob/main/utils/apiFeatures.mjs"
        Sanit["sanitizeData"]:::util
        click Sanit "https://github.com/body661/shoppio-api/blob/main/utils/sanitizeData.mjs"
        Factory["factoryHandler"]:::util
        click Factory "https://github.com/body661/shoppio-api/blob/main/utils/factoryHandler.mjs"
    end
    subgraph "Email Templates"
        WTemplate["Welcome Email"]:::template
        click WTemplate "https://github.com/body661/shoppio-api/blob/main/templates/welcoming.handlebars"
        RTemplate["Password Reset Email"]:::template
        click RTemplate "https://github.com/body661/shoppio-api/blob/main/templates/resetPass.handlebars"
        PCTemplate["Password Changed Notification"]:::template
        click PCTemplate "https://github.com/body661/shoppio-api/blob/main/templates/passChanged.handlebars"
    end
    Mongo["MongoDB Atlas"]:::database
    S3["AWS S3"]:::external
    Stripe["Stripe API"]:::external
    SMTP["SMTP/SES"]:::external

    Client -->|"HTTP(S) request"| LB
    LB -->|"forward request"| SEP
    SEP --> DBInit
    DBInit --> Mongo

    SEP --> UR
    SEP --> AR
    SEP --> PR
    SEP --> OR
    SEP --> CR
    SEP --> CPR
    SEP --> RR
    SEP --> BR
    SEP --> CatR
    SEP --> SubCatR
    SEP --> AdrR
    SEP --> WishR
    SEP --> ValR

    UR --> ValM --> AuthM --> UserC
    AR --> ValM --> AuthM --> AuthC
    PR --> ValM --> AuthM --> ProdC
    OR --> ValM --> AuthM --> OrdC
    CR --> ValM --> AuthM --> CartC
    CPR --> ValM --> AuthM --> CoupC
    RR --> ValM --> AuthM --> RevC
    BR --> ValM --> AuthM --> BrandC
    CatR --> ValM --> AuthM --> CatC
    SubCatR --> ValM --> AuthM --> SubCatC
    AdrR --> ValM --> AuthM --> AdrC
    WishR --> ValM --> AuthM --> WishC
    ValR --> ValM

    AuthC --> UM
    UserC --> UM
    ProdC --> PM
    OrdC --> OM
    CartC --> CaM
    CoupC --> CoM
    RevC --> ReM
    BrandC --> BM
    CatC --> CM
    SubCatC --> SCM
    AdrC --> AM

    AuthC --> JWT
    AuthC --> AF
    UserC --> AF
    ProdC --> Factory
    ProdC --> AF

    UR --> UpM --> ProcM --> S3
    ProcM --> ClnM
    OrdC -->|"process payment"| Stripe
    CoupC -->|"create payment"| Stripe
    * --> EmailU
    WTemplate -.-> EmailU
    RTemplate -.-> EmailU
    PCTemplate -.-> EmailU
    EmailU --> SMTP

    classDef client fill:#ADD8E6,stroke:#000,color:#000
    classDef gateway fill:#B0C4DE,stroke:#000,color:#000
    classDef entry fill:#87CEEB,stroke:#000,color:#000
    classDef config fill:#E0FFFF,stroke:#000,color:#000
    classDef routes fill:#B2DFDB,stroke:#000,color:#000
    classDef middleware fill:#FFE4B5,stroke:#000,color:#000
    classDef controller fill:#C8E6C9,stroke:#000,color:#000
    classDef model fill:#FFCCBC,stroke:#000,color:#000
    classDef util fill:#D1C4E9,stroke:#000,color:#000
    classDef template fill:#FFF9C4,stroke:#000,color:#000
    classDef database fill:#B2EBF2,stroke:#000,color:#000
    classDef external fill:#E0E0E0,stroke:#000,color:#000
```
