# hakanlsk/blog-app architecture

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

- Interactive diagram: https://gitdiagram.com/hakanlsk/blog-app
- Repository: https://github.com/hakanlsk/blog-app

## Overview

##### Project Type and Purpose
- Full-stack web application built on the MERN stack (MongoDB, Express, React, Node.js).
- Purpose: Provide authenticated users the ability to register, log in, create/read/update/delete blog posts, and manage their profile.

##### Main Components
a. Frontend (client/)
– React application (UI layer)
– Routes/pages: Login/Register, Home (post feed), Create/Edit Post, Profile
– State management (local state or context)
– Communication: HTTP calls (fetch or axios) to backend API
b. Backend (server/)
– Node.js + Express server (server/server.js)
– Routes layer (server/routes/*.js)
– Controllers (server/controllers/*.js)
– Helpers (e.g., authHelper.js for JWT operations)
– Models (Mongoose schemas in server/models/*.js)
c. Database
– MongoDB (configured via server/config/db.js, using MONGO_URL from .env)
d. External/Infrastructure Services
– Environment variables (.env via dotenv)
– CORS (cross-origin requests)
– Logging (morgan)
– JWT service for authentication
– bcrypt for password hashing

##### Architectural Patterns & Design Principles
- MVC-ish layering on the backend:
– Models ↔ Controllers ↔ Routes
- Stateless REST API:
– Endpoints protected by JWT middleware (authHelper)
- Separation of concerns:
– Frontend UI isolated from backend API
– Environment/config isolated via dotenv files
- CRUD operations for posts and users

##### Component Relationships & Data Flow
1. User interacts with React UI → triggers API call (HTTP request) →
2. Express route (postRoutes/userRoutes) →
3. Controller (postController/userController) →
4. Model (Mongoose) →
5. MongoDB read/write →
6. Controller responds →
7. Frontend updates state/UI
- Authentication flow:
– Login/Register requests → controller hashes password or compares → on success, generate JWT → send token to client → client stores (localStorage) → attach token to subsequent API calls (Authorization header)

- Clear component labels
- Grouping of related modules (e.g., server/controllers, server/models)
- External dependencies (MongoDB Atlas or self-hosted, JWT issuer, bcrypt)

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend Layer"
        ReactClient["React Client"]:::frontend
    end

    ReactClient -->|"HTTP requests"| ServerEntry
    ServerEntry -->|"HTTP responses"| ReactClient

    subgraph "Express API"
        ServerEntry["server.js"]:::backend
        EnvConfig[".env"]:::config
        DBConfig["db.js"]:::config
        PackageJSON["package.json"]:::config

        Dotenv["dotenv"]:::external
        CORS["CORS"]:::external
        Morgan["morgan"]:::external
        AuthHelper["authHelper.js"]:::external
        JWT["JWT Service"]:::external
        Bcrypt["bcrypt"]:::external

        subgraph "Routes Layer"
            PostRoutes["postRoutes.js"]:::backend
            UserRoutes["userRoutes.js"]:::backend
        end

        subgraph "Controllers Layer"
            PostController["postController.js"]:::backend
            UserController["userController.js"]:::backend
        end

        subgraph "Models Layer"
            PostModel["postModel.js"]:::backend
            UserModel["userModel.js"]:::backend
        end

        ServerEntry -->|uses| EnvConfig
        EnvConfig -->|loads| Dotenv

        ServerEntry -->|connects| DBConfig
        DBConfig -->|connects to| MongoDB

        ServerEntry -->|middleware| CORS
        ServerEntry -->|middleware| Morgan

        ServerEntry -->|registers| PostRoutes
        ServerEntry -->|registers| UserRoutes

        PostRoutes -->|calls| PostController
        UserRoutes -->|calls| UserController

        PostRoutes -->|auth via| AuthHelper
        UserRoutes -->|auth via| AuthHelper

        AuthHelper -->|uses| JWT
        AuthHelper -->|uses| Bcrypt

        PostController -->|uses| PostModel
        UserController -->|uses| UserModel
    end

    MongoDB["MongoDB"]:::database

    click ReactClient "https://github.com/hakanlsk/blog-app/tree/main/client/"
    click ServerEntry "https://github.com/hakanlsk/blog-app/blob/main/server/server.js"
    click EnvConfig "https://github.com/hakanlsk/blog-app/blob/main/server/.env"
    click DBConfig "https://github.com/hakanlsk/blog-app/blob/main/server/config/db.js"
    click PostRoutes "https://github.com/hakanlsk/blog-app/blob/main/server/routes/postRoutes.js"
    click UserRoutes "https://github.com/hakanlsk/blog-app/blob/main/server/routes/userRoutes.js"
    click PostController "https://github.com/hakanlsk/blog-app/blob/main/server/controllers/postController.js"
    click UserController "https://github.com/hakanlsk/blog-app/blob/main/server/controllers/userController.js"
    click PostModel "https://github.com/hakanlsk/blog-app/blob/main/server/models/postModel.js"
    click UserModel "https://github.com/hakanlsk/blog-app/blob/main/server/models/userModel.js"
    click AuthHelper "https://github.com/hakanlsk/blog-app/blob/main/server/helpers/authHelper.js"
    click PackageJSON "https://github.com/hakanlsk/blog-app/blob/main/server/package.json"
    click README["README.md"] "README.md"

    classDef frontend fill:#D0E8FF,stroke:#0366d6,color:#0366d6;
    classDef backend fill:#d0ffd6,stroke:#058a00,color:#058a00;
    classDef config fill:#fff4d6,stroke:#d48806,color:#d48806;
    classDef external fill:#ffe0e0,stroke:#d60000,color:#d60000;
    classDef database fill:#fff0b3,stroke:#b38f00,color:#b38f00;
```
