# aninik4444/amazon-clone architecture

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

- Interactive diagram: https://gitdiagram.com/aninik4444/amazon-clone
- Repository: https://github.com/aninik4444/amazon-clone

## Overview

##### Project Type and Purpose
- This is a full-stack MERN application that clones Amazon’s core shopping flow.
- Front-end: React.js SPA (components, state management, theming, axios utils)
- Back-end: Node.js + Express REST API (routes → controllers → models), connected to MongoDB via Mongoose
- Purpose: Allow users to browse products, filter/sort/search, sign up/log in (JWT auth), add to cart, place orders

##### File-Structure Analysis
- amazon-frontend/
- public/: Static assets (index.html, redirects, manifest)
- src/: App entry (index.js), global CSS, themes, utils (axiosInstance, loader, store)
- components/: UI modules (Home, Product, ProductDetails, Checkout, Orders, Login, SignUp, NavBar, PrivateRoute, ErrorBoundary) each with styling and reducer logic
- backend/
- app.js: Express server setup
- routes/: Defines /api/v1/{products,users,carts,orders} endpoints
- controllers/: Business logic for each resource
- models/: Mongoose schemas (Product, User, Cart, Order)
- middleware/: JWT auth, error handling, 404 handling
- db/connect.js: MongoDB connection
- errors/: Custom error classes

##### Core Architectural Patterns
- Front-end: Component-based UI + Reducer state management (React Context or Redux-style), external service calls via axiosInstance
- Back-end: MVC style (Routes → Controllers → Models), middleware pipeline for auth and error handling
- Security: JWT-based authentication / authorization at the middleware layer
- Data layer: MongoDB as a document store

##### Main Components & Data Flows
a. Front-End SPA (browser)
- User interacts with React components
- Components dispatch actions to local store (cart, search, login reducers)
- UI makes HTTP(S) requests via axiosInstance to API endpoints
b. API Layer (Express server)
- Express Router maps incoming requests to controllers
- Auth middleware validates JWT, then routes to controller
- Controllers perform CRUD operations via Mongoose models
- ErrorHandler middleware catches and formats errors
c. Database (MongoDB Atlas or self-hosted)
- Persists collections: users, products, carts, orders

Data flow example (Add to Cart):
User clicks “Add” → React component dispatches action → axiosInstance.POST /api/v1/cart → auth.js middleware verifies JWT → CartController.create() → CartModel.save() → Respond with updated cart → UI updates store → Cart page re-renders

##### Technology & Deployment Notes
- Front-end: React, React Router, styled-components ( *.styles.js), Context API/reducers
- Back-end: Node.js, Express.js, Mongoose, JWT
- Deployment:
- Front-end can be built and hosted on Netlify / Vercel / S3 + CloudFront
- Back-end containerized via Dockerfile or deployed on Heroku / ECS
- Environment variables (.env) for API URLs, JWT secret/expiry, Mongo connection string

##### Essential Details
- Don’t omit reducers and axiosInstance on the front-end—they’re critical glue
- JWT auth flow in middleware on the server
- Error handling path (errorHandler middleware)
- If time permits, annotate sequence of calls for pagination, search, sort features

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Layer 1: Client/UI (Blue)
    subgraph "UI Layer" 
        direction TB
        IDX["index.js"]:::frontend
        APP["App.js"]:::frontend
        HOME["Home Component"]:::frontend
        PROD_LIST["Product List Component"]:::frontend
        PROD_DETAILS["Product Details Component"]:::frontend
        CHECKOUT["Checkout Component"]:::frontend
        ORDERS["Orders Component"]:::frontend
        LOGIN["Login Component"]:::frontend
        SIGNUP["SignUp Component"]:::frontend
        NAV["NavBar"]:::frontend
        PRIV["PrivateRoute"]:::frontend
        ERR_BOUND["ErrorBoundary"]:::frontend
        STORE["Client-Side Store"]:::frontend
        THEMES["Theming Config"]:::frontend
        LOADER["Global Loader"]:::frontend
        AXIOS["axiosInstance"]:::frontend
    end

    %% Layer 2: Network/API Gateway (Green)
    subgraph "Network/API" 
        direction TB
        AXIS_PROXY["HTTP(S)"]:::api
    end

    %% Layer 3: Server (Green)
    subgraph "Server Layer" 
        direction TB
        APPJS["app.js"]:::server
        DB_CONN["db/connect.js"]:::server
        subgraph "Routes" 
            direction TB
            R_PROD["Product Routes"]:::server
            R_USER["User Routes"]:::server
            R_CART["Cart Routes"]:::server
            R_ORDER["Order Routes"]:::server
        end
        subgraph "Middleware" 
            direction TB
            AUTH["auth.js (JWT Middleware)"]:::server
            ERR_MW["errorHandler.js"]:::server
            NOTF["notFound.js"]:::server
        end
        subgraph "Controllers" 
            direction TB
            C_PROD["ProductController"]:::server
            C_USER["UserController"]:::server
            C_CART["CartController"]:::server
            C_ORDER["OrderController"]:::server
        end
        subgraph "Models" 
            direction TB
            M_PROD["Product Model"]:::server
            M_USER["User Model"]:::server
            M_CART["Cart Model"]:::server
            M_ORDER["Order Model"]:::server
        end
    end

    %% Layer 4: Database (Orange)
    DB["MongoDB"]:::db

    %% Connections UI -> API
    HOME -->|dispatch/action| STORE
    PROD_LIST -->|dispatch/action| STORE
    PROD_DETAILS -->|dispatch/action| STORE
    CHECKOUT -->|dispatch/action| STORE
    ORDERS -->|dispatch/action| STORE
    LOGIN -->|dispatch/action| STORE
    SIGNUP -->|dispatch/action| STORE
    STORE -->|uses| AXIOS
    THEMES -->|provides| APP
    LOADER -->|shows| APP
    APP -->|includes| NAV
    APP -->|wraps| PRIV
    PRIV -->|guards| HOME
    ERR_BOUND -->|catches errors in| APP

    AXIOS -->|calls| AXIS_PROXY

    %% API -> Server
    AXIS_PROXY -->|HTTP requests| APPJS

    %% Server request flow
    APPJS -->|init| DB_CONN
    APPJS -->|use routes| R_PROD
    APPJS -->|use routes| R_USER
    APPJS -->|use routes| R_CART
    APPJS -->|use routes| R_ORDER
    R_PROD -->|goes through| AUTH
    R_USER -->|goes through| AUTH
    R_CART -->|goes through| AUTH
    R_ORDER -->|goes through| AUTH
    AUTH -->|then| C_PROD
    AUTH -->|then| C_USER
    AUTH -->|then| C_CART
    AUTH -->|then| C_ORDER
    C_PROD -->|reads/writes| M_PROD
    C_USER -->|reads/writes| M_USER
    C_CART -->|reads/writes| M_CART
    C_ORDER -->|reads/writes| M_ORDER
    M_PROD -->|persist| DB
    M_USER -->|persist| DB
    M_CART -->|persist| DB
    M_ORDER -->|persist| DB
    any_error((Error)) -->|handled by| ERR_MW
    APPJS -->|404s to| NOTF

    %% Click Events
    click IDX "https://github.com/aninik4444/amazon-clone/blob/main/amazon-frontend/src/index.js"
    click APP "https://github.com/aninik4444/amazon-clone/blob/main/amazon-frontend/src/App.js"
    click HOME "https://github.com/aninik4444/amazon-clone/blob/main/amazon-frontend/src/components/Home/index.js"
    click PROD_LIST "https://github.com/aninik4444/amazon-clone/blob/main/amazon-frontend/src/components/Product/index.js"
    click PROD_DETAILS "https://github.com/aninik4444/amazon-clone/blob/main/amazon-frontend/src/components/ProductDetails/index.js"
    click CHECKOUT "https://github.com/aninik4444/amazon-clone/blob/main/amazon-frontend/src/components/Checkout/index.js"
    click ORDERS "https://github.com/aninik4444/amazon-clone/blob/main/amazon-frontend/src/components/Orders/index.js"
    click LOGIN "https://github.com/aninik4444/amazon-clone/blob/main/amazon-frontend/src/components/Login/index.js"
    click SIGNUP "https://github.com/aninik4444/amazon-clone/blob/main/amazon-frontend/src/components/SignUp/index.js"
    click NAV "https://github.com/aninik4444/amazon-clone/blob/main/amazon-frontend/src/components/NavBar/index.js"
    click PRIV "https://github.com/aninik4444/amazon-clone/blob/main/amazon-frontend/src/components/PrivateRoute/index.js"
    click ERR_BOUND "https://github.com/aninik4444/amazon-clone/blob/main/amazon-frontend/src/components/ErrorBoundary/index.js"
    click STORE "https://github.com/aninik4444/amazon-clone/blob/main/amazon-frontend/src/utils/store/index.js"
    click THEMES "https://github.com/aninik4444/amazon-clone/blob/main/amazon-frontend/src/themes/index.js"
    click LOADER "https://github.com/aninik4444/amazon-clone/blob/main/amazon-frontend/src/utils/loader/index.js"
    click AXIOS "https://github.com/aninik4444/amazon-clone/blob/main/amazon-frontend/src/utils/axiosInstance.js"
    click APPJS "https://github.com/aninik4444/amazon-clone/blob/main/backend/app.js"
    click DB_CONN "https://github.com/aninik4444/amazon-clone/blob/main/backend/db/connect.js"
    click R_PROD "https://github.com/aninik4444/amazon-clone/blob/main/backend/routes/Product.js"
    click R_USER "https://github.com/aninik4444/amazon-clone/blob/main/backend/routes/User.js"
    click R_CART "https://github.com/aninik4444/amazon-clone/blob/main/backend/routes/Cart.js"
    click R_ORDER "https://github.com/aninik4444/amazon-clone/blob/main/backend/routes/Order.js"
    click AUTH "https://github.com/aninik4444/amazon-clone/blob/main/backend/middleware/auth.js"
    click ERR_MW "https://github.com/aninik4444/amazon-clone/blob/main/backend/middleware/errorHandler.js"
    click NOTF "https://github.com/aninik4444/amazon-clone/blob/main/backend/middleware/notFound.js"
    click C_PROD "https://github.com/aninik4444/amazon-clone/blob/main/backend/controllers/Product.js"
    click C_USER "https://github.com/aninik4444/amazon-clone/blob/main/backend/controllers/User.js"
    click C_CART "https://github.com/aninik4444/amazon-clone/blob/main/backend/controllers/Cart.js"
    click C_ORDER "https://github.com/aninik4444/amazon-clone/blob/main/backend/controllers/Order.js"
    click M_PROD "https://github.com/aninik4444/amazon-clone/blob/main/backend/models/Product.js"
    click M_USER "https://github.com/aninik4444/amazon-clone/blob/main/backend/models/User.js"
    click M_CART "https://github.com/aninik4444/amazon-clone/blob/main/backend/models/Cart.js"
    click M_ORDER "https://github.com/aninik4444/amazon-clone/blob/main/backend/models/Order.js"

    %% Styles
    classDef frontend fill:#E3F2FD,stroke:#1976D2,stroke-width:2px
    classDef api fill:#C8E6C9,stroke:#388E3C,stroke-width:2px
    classDef server fill:#A5D6A7,stroke:#2E7D32,stroke-width:2px
    classDef db fill:#FFE0B2,stroke:#F57C00,stroke-width:2px
```
