# harshvyas0803/hub architecture

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

- Interactive diagram: https://gitdiagram.com/harshvyas0803/hub
- Repository: https://github.com/harshvyas0803/hub

## Overview

##### Project Overview
- This is a full-stack web application for a blog platform where users can create, read, update, and delete blog posts and also interact via likes/dislikes and comments.
- The project uses the MERN stack, which means the backend is powered by Node.js and Express, the database is MongoDB, and the frontend is built with React (using Vite and Tailwind CSS for added styling).

##### Main Components
a. Frontend:
- Located in the “frontend” directory.
- Contains React components, pages, and context files.
- Provides the user interface and handles client-side routing using components like Navbar, Sidebar, and various page components (Home, BlogDetail, CreateBlog, etc.).
- Assets include images, a custom animated loader, and configuration files such as “vite.config.js”.

b. Backend:
- Located in the “backend” directory.
- Consists of an ExpressJS server entry point (“index.js”), middleware (for instance, the authMiddleware for authentication), models for the database (Category, Post, User), routes (authRoutes, categoryRoutes, postRoutes), and seed files (seed script and use-once seed files) for populating initial data.
- Handles server-side routes, business logic, and API endpoints for the blog operations.

c. Database:
- As described in the README under the tech stack, MongoDB is used as the database.
- The backend models indicate the structure of collections (users, posts, categories) and their interactions.

##### Relationships and Data Flow
- Data flows from the frontend to the backend through RESTful API calls. For instance, when a user creates or reads a blog post, the React application communicates with the Express API routes.
- The backend processes these requests, interacts with MongoDB (reading/writing documents via Mongoose models), and sends responses back to the frontend.
- Authentication and authorization are managed using middleware in the backend to secure specific endpoints.
- External services could be included if any third-party authentication or media storage is integrated, although the current tree focuses on internal interactions.

##### Architectural Patterns and Design Principles
- Separation of Concerns: The project distinctly separates the frontend (React UI) from the backend (Express API), following a client-server architecture.
- MVC-like Pattern in Backend: The “models” directory holds data structures, “routes” directory handles API endpoints (controllers), and “middleware” provides request preprocessing (such as authentication).
- Use of Context in React: The frontend utilizes React’s Context API (e.g., BlogContext) to manage global state which supports a smoother UI/UX.
- Environment-based Configuration: The backend makes use of .env file for environment variables, ensuring clean separation of configurations.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Subgraph
    subgraph "Frontend"
        FE_MAIN["Frontend (React UI)"]:::frontend
        FE_PAGES["Pages (Home, BlogDetail, CreateBlog, etc.)"]:::frontend
        FE_NAVBAR["Navbar"]:::frontend
        FE_SIDEBAR["Sidebar"]:::frontend
        FE_FETCHED["FetchedCards"]:::frontend
        FE_RSIDEBAR["RightSidebar"]:::frontend
        FE_CONTEXT["BlogContext (React Context API)"]:::frontend
        FE_CONFIG["Vite & Tailwind Config"]:::frontend

        FE_MAIN --> FE_PAGES
        FE_MAIN --> FE_NAVBAR
        FE_MAIN --> FE_SIDEBAR
        FE_MAIN --> FE_FETCHED
        FE_MAIN --> FE_RSIDEBAR
        FE_MAIN --> FE_CONTEXT
        FE_MAIN --> FE_CONFIG
    end

    %% Backend Subgraph
    subgraph "Backend"
        BE_MAIN["Backend (Express API Server)"]:::backend
        BE_ENTRY["Entry Point (index.js)"]:::backend
        BE_MIDDLEWARE["Middleware (authMiddleware)"]:::backend
        %% Models Subgraph
        subgraph "Models"
            BE_CATEGORY["Category Model"]:::backend
            BE_POST["Post Model"]:::backend
            BE_USER["User Model"]:::backend
        end
        %% Routes Subgraph
        subgraph "Routes"
            BE_AUTHROUTES["Auth Routes"]:::backend
            BE_CATEGORYROUTES["Category Routes"]:::backend
            BE_POSTROUTES["Post Routes"]:::backend
        end
        %% Seed Scripts Subgraph
        subgraph "Seed Scripts"
            BE_SEED_MAIN["Seed Script (seed.js)"]:::backend
            BE_SEED_ONE["Seed Categories (seedCategories.js)"]:::backend
        end
        BE_ENV["Environment Config (.env)"]:::backend

        BE_MAIN --> BE_ENTRY
        BE_MAIN --> BE_MIDDLEWARE
        BE_MAIN --> BE_CATEGORY
        BE_MAIN --> BE_POST
        BE_MAIN --> BE_USER
        BE_MAIN --> BE_AUTHROUTES
        BE_MAIN --> BE_CATEGORYROUTES
        BE_MAIN --> BE_POSTROUTES
        BE_MAIN --> BE_SEED_MAIN
        BE_MAIN --> BE_SEED_ONE
        BE_MAIN --> BE_ENV

        %% Internal Relationships in Backend
        BE_MIDDLEWARE -->|"secures"| BE_AUTHROUTES
        BE_AUTHROUTES -->|"uses"| BE_CATEGORY
        BE_AUTHROUTES -->|"uses"| BE_POST
        BE_AUTHROUTES -->|"uses"| BE_USER
    end

    %% Database Node
    DB["Database (MongoDB)"]:::database

    %% External Interactions
    FE_MAIN -->|"HTTP_API_Calls(CRUD,like/dislike,comments)"| BE_MAIN
    BE_MAIN -->|"Data_operations(create,read,update,delete)"| DB

    %% Click Events for Frontend Components
    click FE_MAIN "https://github.com/harshvyas0803/hub/tree/main/frontend"
    click FE_PAGES "https://github.com/harshvyas0803/hub/tree/main/frontend/src/pages"
    click FE_NAVBAR "https://github.com/harshvyas0803/hub/blob/main/frontend/src/components/navbar/Navbar.jsx"
    click FE_SIDEBAR "https://github.com/harshvyas0803/hub/blob/main/frontend/src/components/sidebar/Sidebar.jsx"
    click FE_FETCHED "https://github.com/harshvyas0803/hub/blob/main/frontend/src/components/fetchedcards/FetchedCards.jsx"
    click FE_RSIDEBAR "https://github.com/harshvyas0803/hub/blob/main/frontend/src/components/rightsidebar/RightSidebar.jsx"
    click FE_CONTEXT "https://github.com/harshvyas0803/hub/blob/main/frontend/src/context/BlogContext.jsx"
    click FE_CONFIG "https://github.com/harshvyas0803/hub/blob/main/frontend/vite.config.js"

    %% Click Events for Backend Components
    click BE_MAIN "https://github.com/harshvyas0803/hub/tree/main/backend"
    click BE_ENTRY "https://github.com/harshvyas0803/hub/blob/main/backend/index.js"
    click BE_MIDDLEWARE "https://github.com/harshvyas0803/hub/blob/main/backend/middleware/authMiddleware.js"
    click BE_CATEGORY "https://github.com/harshvyas0803/hub/blob/main/backend/models/Category.js"
    click BE_POST "https://github.com/harshvyas0803/hub/blob/main/backend/models/Post.js"
    click BE_USER "https://github.com/harshvyas0803/hub/blob/main/backend/models/User.js"
    click BE_AUTHROUTES "https://github.com/harshvyas0803/hub/blob/main/backend/routes/authRoutes.js"
    click BE_CATEGORYROUTES "https://github.com/harshvyas0803/hub/blob/main/backend/routes/categoryRoutes.js"
    click BE_POSTROUTES "https://github.com/harshvyas0803/hub/blob/main/backend/routes/postRoutes.js"
    click BE_SEED_MAIN "https://github.com/harshvyas0803/hub/blob/main/backend/seed.js"
    click BE_SEED_ONE "https://github.com/harshvyas0803/hub/blob/main/backend/useoncefiles/seedCategories.js"
    click BE_ENV "https://github.com/harshvyas0803/hub/blob/main/backend/.env"

    %% Styles
    classDef frontend fill:#add8e6,stroke:#333,stroke-width:2px;
    classDef backend fill:#90ee90,stroke:#333,stroke-width:2px;
    classDef database fill:#ffcc99,stroke:#333,stroke-width:2px;
```
