# ankur-git-commit/mern-goalsetterapp architecture

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

- Interactive diagram: https://gitdiagram.com/ankur-git-commit/mern-goalsetterapp
- Repository: https://github.com/ankur-git-commit/mern-goalsetterapp

## Overview

##### Key Components
- Frontend: Built with React and Redux Toolkit. It contains components (GoalForm, GoalItem, Header, Spinner), feature slices (auth and goals), different pages (Dashboard, Login, Register), and uses Vite as the build tool.
- Backend: A Node.js/Express API that includes configuration (db.js), controllers (userController, goalController), middleware (auth, error handling), models (Mongoose models for users and goals), and RESTful routes.
- Database: MongoDB connected via Mongoose.
- External Services: JWT for authentication, Bcrypt for password hashing, and React Toastify for notifications in the frontend.

##### Out Relationships and Interactions
- Frontend and Backend Interaction: The React frontend makes HTTP requests (using Axios) to the backend API endpoints (e.g., /api/users, /api/goals). The API endpoints are protected by JWT-based authentication.
- State Management: In the frontend, Redux Toolkit manages state (with separate slices for auth and goals), where actions trigger updates based on responses from the backend.
- Data Flow: User actions in React components lead to API calls, which are handled by controllers in the backend. The controllers then interact with the MongoDB database via Mongoose models.
- Middleware: The backend middleware layer (authMiddleware and errorMiddleware) processes requests for authentication and error handling.

##### Architectural Patterns and Key Principles
- Clear separation between the client-side UI and backend API.
- MVC-leaning structure on the backend (Controllers, Models, Routes).
- Middleware pattern for centralizing cross-cutting concerns like authentication and error handling.
- RESTful design for API endpoints.
- State management with Redux in the frontend to handle global state in a modular way.

##### Technology Stack Details
- Backend Technologies: Node.js, Express.js, Mongoose/MongoDB, JWT, Bcrypt.js, Express Async Handler.
- Build/Dev Tools: Vite for frontend and concurrently for running both parts (backend and frontend) together.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% Frontend Cluster
    subgraph "Frontend"
        subgraph "Components"
            GF["GoalForm"]:::frontend
            GI["GoalItem"]:::frontend
            HD["Header"]:::frontend
            SP["Spinner"]:::frontend
        end
        subgraph "Pages"
            DASH["Dashboard Page"]:::frontend
            LOGIN["Login Page"]:::frontend
            REG["Register Page"]:::frontend
        end
        subgraph "Redux State"
            AS["Auth Slice"]:::frontend
            GS["Goals Slice"]:::frontend
            AUTHSVC["Auth Service"]:::frontend
            GOALSVC["Goals Service"]:::frontend
            STORE["Redux Store"]:::frontend
        end
    end

    %% Backend Cluster
    subgraph "Backend"
        ES["Express Server"]:::backend
        subgraph "Routes"
            UR["User Routes"]:::backend
            GR["Goal Routes"]:::backend
        end
        subgraph "Controllers"
            UC["User Controller"]:::backend
            GC["Goal Controller"]:::backend
        end
        subgraph "Middleware"
            AM["Authentication Middleware"]:::backend
            EM["Error Middleware"]:::backend
        end
        subgraph "Models"
            UM["User Model"]:::backend
            GM["Goal Model"]:::backend
        end
        DBC["Database Config"]:::backend
    end

    %% Infrastructure Cluster
    subgraph "Infrastructure"
        MDB["MongoDB Database"]:::database
    end

    %% External Services Cluster
    subgraph "External Services"
        JWT["JWT"]:::external
        BCRYPT["Bcrypt"]:::external
        TOAST["React Toastify"]:::external
        AXIOS["Axios"]:::external
    end

    %% Connections between Frontend and External Services
    DASH -->|"calls_API"| AXIOS
    LOGIN -->|"calls_API"| AXIOS
    REG -->|"calls_API"| AXIOS
    STORE -->|"dispatch_via"| AXIOS
    HD -->|"notifies"| TOAST

    %% Frontend to Backend Communication
    DASH -->|"HTTP_request"| ES
    LOGIN -->|"HTTP_request"| ES
    REG -->|"HTTP_request"| ES

    %% Backend Initialization and DB Connection
    ES -->|"initDB"| DBC
    DBC -->|"connects"| MDB

    %% Express Server dispatches to Routes
    ES -->|"dispatch_user"| UR
    ES -->|"dispatch_goal"| GR

    %% Routes use Middleware for authentication then forward to Controllers
    UR -->|"uses"| AM
    GR -->|"uses"| AM
    AM --> UC
    AM --> GC

    %% Controllers Interact with Models
    UC -->|"uses_model"| UM
    GC -->|"uses_model"| GM

    %% Backend Middleware interactions
    AM -->|"uses"| JWT
    AM -->|"uses"| BCRYPT

    %% Click Events for Frontend Components
    click GF "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/frontend/src/components/GoalForm.jsx"
    click GI "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/frontend/src/components/GoalItem.jsx"
    click HD "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/frontend/src/components/Header.jsx"
    click SP "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/frontend/src/components/Spinner.jsx"

    %% Click Events for Frontend Pages
    click DASH "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/frontend/src/pages/Dashboard.jsx"
    click LOGIN "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/frontend/src/pages/Login.jsx"
    click REG "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/frontend/src/pages/Register.jsx"

    %% Click Events for Frontend State Management
    click AS "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/frontend/src/features/auth/authSlice.js"
    click GS "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/frontend/src/features/goals/goalSlice.js"
    click AUTHSVC "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/frontend/src/features/auth/authService.js"
    click GOALSVC "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/frontend/src/features/goals/goalService.js"
    click STORE "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/frontend/src/app/store.js"

    %% Click Events for Backend Server and Config
    click ES "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/backend/server.js"
    click DBC "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/backend/config/db.js"

    %% Click Events for Backend Controllers
    click UC "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/backend/controller/userController.js"
    click GC "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/backend/controller/goalController.js"

    %% Click Events for Backend Middleware
    click AM "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/backend/middleware/authMiddleware.js"
    click EM "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/backend/middleware/errorMiddleware.js"

    %% Click Events for Backend Models
    click UM "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/backend/models/userModel.js"
    click GM "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/backend/models/goalModel.js"

    %% Click Events for Backend Routes
    click UR "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/backend/routes/userRoutes.js"
    click GR "https://github.com/ankur-git-commit/mern-goalsetterapp/blob/main/backend/routes/goalRoutes.js"

    %% Styling Classes
    classDef frontend fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef backend fill:#d4edda,stroke:#333,stroke-width:2px;
    classDef database fill:#f8d7da,stroke:#333,stroke-width:2px;
    classDef external fill:#fff3cd,stroke:#333,stroke-width:2px;
```
