# saurabh-8585/mern-e-commerce-backend architecture

> Architecture diagram of the GitHub repository saurabh-8585/mern-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/saurabh-8585/mern-e-commerce-backend
- Repository: https://github.com/saurabh-8585/mern-e-commerce-backend

## Overview

##### Project type and purpose
- This repository implements the backend of a MERN-stack e-commerce application.
- It serves a React/Material-UI frontend (separate repo) by exposing RESTful endpoints for user auth, product/catalog operations, cart & order management, payments, reviews, and administration.

##### File structure
- Top-level entry point: index.js (bootstraps Express, mounts routes, connects to MongoDB).
- config.js: application configuration (MongoDB URI, JWT secrets, payment gateway keys).
- controllers/: business-logic layer (AdminControl, AllProductInfo, forgotPasswordController, paymentController, deleteUser, etc.).
- routes/: Express routers broken out by feature (auth.js, product.js, cart.js, paymentRoute.js, review.js, wishlist.js, plus an Admin subfolder).
- middleware/: JWT-based API authentication, admin/user role guards.
- models/: Mongoose schemas (User, Product, Cart, Wishlist, Review, Payment).
- Deployment config: vercel.json (serverless or deployment settings).

##### README for additional insights
- Confirms use of MongoDB, NodeJS, Express, JWT auth, payment gateway integration, mailing service.
- Frontend uses React, ContextAPI, React-Router—so the client invokes your API via HTTP/HTTPS.

##### Sketch the high-level components and interactions
a. Main components
- Client (React SPA hosted at e-shopit.vercel.app)
- API Server (Node.js + Express)
- MongoDB Atlas (or your chosen MongoDB host)
- External Services:
– Payment Gateway (e.g., Stripe, PayPal)
– Mail Service (e.g., SendGrid, NodeMailer + SMTP)
– Cloudinary (for image uploads & hosting)
b. Relationships & data flows
1. User ↔ Client (HTTPS requests, JWT stored in client)
2. Client → API Gateway (Express routes: /auth, /products, /cart …)
3. API Gateway → Auth Middleware → Controller → Model → MongoDB
4. PaymentController ↔ Payment Gateway API
5. forgotPasswordController ↔ Mail Service API
6. Controllers ↔ Cloudinary SDK for image upload URLs
c. Architectural patterns & principles
– MVC-inspired layering: Routes (entry points) → Middleware (cross-cutting concerns) → Controllers (use-case logic) → Models (persistence)
– Stateless JWT-based authentication (scalable horizontally)
– Clear separation of concerns: admin vs user routes via authAdmin/authUser middleware
d. Key technologies
– ExpressJS (web framework)
– Mongoose (ODM)
– JWT (authentication)
– MongoDB
– External SDKs (Payment, Mail, Cloudinary)

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        ReactSPA["ReactJS SPA"]:::client
    end

    subgraph "Server (Node.js + Express)"
        Index["index.js"]:::core
        Config["config.js"]:::core
        Vercel["vercel.json"]:::core

        subgraph Routes
            AuthRoute["auth.js"]:::core
            ProductRoute["product.js"]:::core
            CartRoute["cart.js"]:::core
            PaymentRoute["paymentRoute.js"]:::core
            ReviewRoute["review.js"]:::core
            WishlistRoute["wishlist.js"]:::core
            ForgotRoute["forgotPassword.js"]:::core
            AdminRoute["AdminAuth.js"]:::core
        end

        subgraph Middleware
            ApiAuth["apiAuth.js"]:::middleware
            AuthUser["authUser.js"]:::middleware
            AuthAdmin["authAdmin.js"]:::middleware
        end

        subgraph Controllers
            AdminControl["AdminControl.js"]:::core
            AllProduct["AllProductInfo.js"]:::core
            DeleteUser["deleteUser.js"]:::core
            ForgotController["forgotPasswordController.js"]:::core
            PaymentController["paymentController.js"]:::core
        end

        subgraph Models
            UserModel["User.js"]:::core
            ProductModel["Product.js"]:::core
            CartModel["Cart.js"]:::core
            WishlistModel["Wishlist.js"]:::core
            ReviewModel["Review.js"]:::core
            PaymentModel["Payment.js"]:::core
        end
    end

    subgraph Database
        MongoDB["MongoDB Atlas"]:::database
    end

    subgraph "External Services"
        PaymentGateway["Payment Gateway"]:::external
        MailService["Mail Service"]:::external
        Cloudinary["Cloudinary"]:::external
    end

    ReactSPA -->|"HTTPS"| Index
    Index --> Config
    Index --> AuthRoute
    Index --> ProductRoute
    Index --> CartRoute
    Index --> PaymentRoute
    Index --> ReviewRoute
    Index --> WishlistRoute
    Index --> ForgotRoute
    Index --> AdminRoute
    Index --> Vercel

    AuthRoute --> ApiAuth
    ProductRoute --> ApiAuth
    CartRoute --> ApiAuth
    PaymentRoute --> ApiAuth
    ReviewRoute --> ApiAuth
    WishlistRoute --> ApiAuth
    ForgotRoute --> ApiAuth
    AdminRoute --> AuthAdmin

    ApiAuth --> Controllers
    AuthUser --> Controllers
    AuthAdmin --> Controllers

    Controllers --> Models
    Models --> MongoDB

    PaymentController --> PaymentGateway
    ForgotController --> MailService
    Controllers --> Cloudinary

    click Index "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/index.js"
    click Config "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/config.js"
    click Vercel "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/vercel.json"
    click AdminControl "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/controller/AdminControl.js"
    click AllProduct "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/controller/AllProductInfo.js"
    click DeleteUser "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/controller/deleteUser.js"
    click ForgotController "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/controller/forgotPasswordController.js"
    click PaymentController "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/controller/paymentController.js"
    click AuthRoute "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/routes/auth.js"
    click ProductRoute "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/routes/product.js"
    click CartRoute "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/routes/cart.js"
    click PaymentRoute "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/routes/paymentRoute.js"
    click ReviewRoute "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/routes/review.js"
    click WishlistRoute "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/routes/wishlist.js"
    click ForgotRoute "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/routes/forgotPassword.js"
    click AdminRoute "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/routes/Admin/AdminAuth.js"
    click ApiAuth "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/middleware/apiAuth.js"
    click AuthUser "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/middleware/authUser.js"
    click AuthAdmin "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/middleware/authAdmin.js"
    click UserModel "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/models/User.js"
    click ProductModel "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/models/Product.js"
    click CartModel "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/models/Cart.js"
    click WishlistModel "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/models/Wishlist.js"
    click ReviewModel "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/models/Review.js"
    click PaymentModel "https://github.com/saurabh-8585/mern-e-commerce-backend/blob/master/models/Payment.js"

    classDef core fill:#cce5ff,stroke:#4d8bfd;
    classDef middleware fill:#ffe0b3,stroke:#ffb84d;
    classDef client fill:#e6f7ff,stroke:#66c2ff;
    classDef database fill:#fff2cc,stroke:#ffd966;
    classDef external fill:#d9ead3,stroke:#8cc152;
```
