# aliftalha/mern architecture

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

- Interactive diagram: https://gitdiagram.com/aliftalha/mern
- Repository: https://github.com/aliftalha/mern

## Overview

##### Project type and purpose
- This is a full-stack MERN-stack fitness tracker app.
- Purpose: Allow users to register/login (JWT auth), log cardio and resistance workouts, view workout history, calculate BMI, and manage exercises (create/delete).

##### File structure
- Top level:
- backend/ – Express.js REST API
- frontend/ – React.js single-page application
- backend/ pattern:
- config/db.js – Mongoose/MongoDB connection
- middleware/auth.middleware.js – JWT verification
- controllers/ – route handlers (auth.controller.js, exercise.controller.js, resistance-controller.js, workoutController.js, user.controller.js)
- models/ – Mongoose schemas (User, Workout, Exercise, Resistance, Comment)
- routes/ – express.Router modules mounted on `/api/auth`, `/api/workouts`, `/api/exercises`, `/api/resistance`, `/api/comments`, `/api/users`
- utils/errorHandler.js – centralized error formatting
- server.js – app setup, middleware registration, route mounting, listen
- frontend/ pattern:
- public/ – static index.html, manifest
- src/
– pages/ – React Router pages (Home, About, BMI, ContactUs, Dashboard, Details, Login, Register, Resistance)
– components/ – reusable UI (Navbar, Footer, ExerciseList/Form, WorkoutList/Form/Details)
– context/ – React Context providers (AuthContext, WorkoutContext)
– hooks/useWorkoutsContext.js – custom hook for global state
– utils/api.js – axios instance with baseURL + token injection
– App.js + index.js – root component, routing

##### README for additional insights
- Technology stack: Mongoose, Express, React, Node, JWT Authentication
- Key features: workout logging (cardio/resistance), history view, delete operations, BMI calculator, standard pages (About, Contact Us), authentication flows.

a. Main components
- Client-side (React SPA)
- API Gateway (Express server)
- Authentication layer (JWT middleware)
- Business logic (controllers)
- Data Access (Mongoose models)
- Database (MongoDB)
- External services (e.g., hosting/CDN, if any)
b. Relationships and interactions
1. User (browser/mobile) → React SPA (HTTP over HTTPS)
2. React SPA → axiosInstance (in utils) attaches JWT token → Express API endpoints
3. Express routes → auth middleware (for protected routes) → controllers
4. Controllers ↔ models (Mongoose) → MongoDB Atlas/local
5. Responses flow back along the chain to the client
c. Architectural patterns and principles
- MVC-style layering on backend (Routes → Controllers → Models)
- Single Page Application with React Router on frontend
- Context API for global state management (AuthContext, WorkoutContext)
- Custom hooks for data fetching and state updates
- Centralized error handling in backend (utils/errorHandler)
- Stateless JWT auth for session management
d. Key technologies/frameworks
- React.js, React Router, Context API, axios
- Node.js, Express.js, JWT, Mongoose, MongoDB

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Frontend (React SPA)"
        direction TB
        User["User Browser"]:::external
        A1["index.js"]:::frontend
        A2["App.js"]:::frontend
        subgraph "Pages"
            direction TB
            P1["Home.js"]:::frontend
            P2["About.js"]:::frontend
            P3["BMI.js"]:::frontend
            P4["ContactUs.js"]:::frontend
            P5["Dashboard.js"]:::frontend
            P6["Details.js"]:::frontend
            P7["Login.js"]:::frontend
            P8["Register.js"]:::frontend
            P9["Resistance.js"]:::frontend
        end
        subgraph "Components"
            direction TB
            C1["Navbar.js"]:::frontend
            C2["Footer.js"]:::frontend
            C3["ExerciseList.js"]:::frontend
            C4["ExerciseForm.js"]:::frontend
            C5["WorkoutList.js"]:::frontend
            C6["WorkoutForm.js"]:::frontend
            C7["WorkoutDetails.js"]:::frontend
        end
        subgraph "Context & Hooks"
            direction TB
            CT1["AuthContext.js"]:::frontend
            CT2["WorkoutContext.js"]:::frontend
            H1["useWorkoutsContext.js"]:::frontend
        end
        U1["api.js (Axios)"]:::frontend
    end

    %% Server Layer
    subgraph "Backend (Express API)"
        direction TB
        B1["server.js"]:::backend
        B2["db.js"]:::backend
        B3["auth.middleware.js"]:::backend
        B4["errorHandler.js"]:::backend

        subgraph "Routes"
            direction TB
            R1["auth.js (/api/auth)"]:::backend
            R2["users.js (/api/users)"]:::backend
            R3["workouts.js (/api/workouts)"]:::backend
            R4["exercises.js (/api/exercises)"]:::backend
            R5["resistance.js (/api/resistance)"]:::backend
            R6["comments.js (/api/comments)"]:::backend
        end

        subgraph "Controllers"
            direction TB
            C01["auth.controller.js"]:::backend
            C02["exercise.controller.js"]:::backend
            C03["resistance-controller.js"]:::backend
            C04["workoutController.js"]:::backend
            C05["user.controller.js"]:::backend
        end

        subgraph "Models"
            direction TB
            M1["user.model.js"]:::data
            M2["workoutModel.js"]:::data
            M3["exercise.model.js"]:::data
            M4["Resistance.js"]:::data
            M5["Comment.js"]:::data
        end
    end

    %% Database
    DB["MongoDB"]:::db

    %% Connections
    User -->|HTTPS| A1
    A1 --> A2
    A2 -->|"wraps with Context Providers"| CT1
    A2 -->|"wraps with Context Providers"| CT2
    CT2 --> H1
    A2 --> U1
    U1 -->|REST /api/* with JWT| B1
    B1 --> B2
    B1 --> B3
    B1 --> B4
    B1 --> R1
    B1 --> R2
    B1 --> R3
    B1 --> R4
    B1 --> R5
    B1 --> R6
    R1 --> C01
    R2 --> C05
    R3 --> C04
    R4 --> C02
    R5 --> C03
    R6 --> C05
    C01 --> M1
    C02 --> M3
    C03 --> M4
    C04 --> M2
    C06["controllers ↔ models"]:::note
    C05 --> M1
    M1 --> DB
    M2 --> DB
    M3 --> DB
    M4 --> DB
    M5 --> DB

    %% Click Events
    click B1 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/server.js"
    click B2 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/config/db.js"
    click B3 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/middleware/auth.middleware.js"
    click B4 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/utils/errorHandler.js"
    click C01 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/controllers/auth.controller.js"
    click C02 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/controllers/exercise.controller.js"
    click C03 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/controllers/resistance-controller.js"
    click C04 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/controllers/workoutController.js"
    click C05 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/controllers/user.controller.js"
    click M1 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/models/user.model.js"
    click M2 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/models/workoutModel.js"
    click M3 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/models/exercise.model.js"
    click M4 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/models/Resistance.js"
    click M5 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/models/Comment.js"
    click R1 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/routes/auth.js"
    click R2 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/routes/users.js"
    click R3 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/routes/workouts.js"
    click R4 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/routes/exercises.js"
    click R5 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/routes/resistance.js"
    click R6 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/backend/routes/comments.js"
    click A1 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/index.js"
    click A2 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/App.js"
    click P1 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/pages/Home.js"
    click P2 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/pages/About.js"
    click P3 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/pages/BMI.js"
    click P4 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/pages/ContactUs.js"
    click P5 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/pages/Dashboard.js"
    click P6 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/pages/Details.js"
    click P7 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/pages/Login.js"
    click P8 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/pages/Register.js"
    click P9 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/pages/Resistance.js"
    click C1 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/components/Navbar.js"
    click C2 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/components/Footer.js"
    click C3 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/components/ExerciseList.js"
    click C4 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/components/ExerciseForm.js"
    click C5 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/components/WorkoutList.js"
    click C6 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/components/WorkoutForm.js"
    click C7 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/components/WorkoutDetails.js"
    click CT1 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/context/AuthContext.js"
    click CT2 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/context/WorkoutContext.js"
    click H1 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/hooks/useWorkoutsContext.js"
    click U1 "https://github.com/aliftalha/mern/blob/main/Fitness_Project/frontend/src/utils/api.js"

    %% Styles
    classDef frontend fill:#D0E6FF,stroke:#0366d6;
    classDef backend fill:#E6FFED,stroke:#22863a;
    classDef data fill:#FFFAE6,stroke:#dbab09;
    classDef db fill:#FFEFEF,stroke:#d73a49;
    classDef external fill:#F0F0F0,stroke:#999999,stroke-dasharray: 5 2;
    classDef note fill:#FFFFFF,stroke:#CCCCCC,stroke-dasharray: 2 2;
    class C06 note
```
