# yogeshawasthi/blog_backend_in_mern architecture

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

- Interactive diagram: https://gitdiagram.com/yogeshawasthi/blog_backend_in_mern
- Repository: https://github.com/yogeshawasthi/blog_backend_in_mern

## Overview

##### Project type and purpose
- This is the backend of a MERN-stack blog application. Its sole responsibility is to expose RESTful endpoints for authentication, user management, and blog operations, backed by MongoDB.

##### File structure
- Index.js: server entry point
- config/db.js: database connection logic
- routes/*.js: Express router definitions for auth, blog, and user endpoints
- controllers/*Controller.js: business logic for each route group
- models/*.js: Mongoose schemas for User and Blog
- middlewares/authMiddlewares.js: JWT-based request authentication/authorization
- utils/jwt.js: token creation and verification helper

a. Client (React or any HTTP client)
b. Express API server
- Routing layer
- Authentication middleware
- Controllers (Auth, User, Blog)
c. MongoDB database (via Mongoose models)
d. JWT utility service (token issuance and validation)

##### Relationships and data flows
- Client issues HTTP requests (GET/POST/PUT/DELETE) → Express routing layer
- Requests pass through auth middleware when needed → Controllers handle business rules → Controllers invoke Mongoose models to read/write MongoDB → Responses bubble back to client
- AuthController uses utils/jwt to sign and verify tokens
- config/db.js establishes and maintains the MongoDB connection

##### Architectural patterns and design principles
- Layered architecture (Routes → Middleware → Controllers → Data Access)
- Separation of concerns: authentication logic isolated in middleware and utils, business logic in controllers, persistence in models
- Stateless JWT authentication

##### Technologies and frameworks
- Node.js, Express.js
- MongoDB with Mongoose ODM
- JSON Web Tokens (jwt)
- dotenv or custom config for environment variables (in config/db.js)

##### Detailed breakdown
- Client layer
- React App → “HTTP requests with JWT in Authorization header”
- API Server layer
1. Index.js starts the server and loads db.js
2. Routes (authRoutes, userRoutes, blogRoutes) dispatch to:
a. Authentication middleware (protects routes)
b. Controllers (authControllers, userController, blogController)
3. Controllers call Mongoose models (user.js, blog.js)
- Persistence layer
- MongoDB database (collections: users, blogs)
- Utility service
- jwt.js for sign/verify operations

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Layer
    Client["React App / HTTP Client"]:::client

    %% API Server Entry and DB Connection
    subgraph "API Server"
        direction TB
        Index["Index.js"]:::server
        DBConn["config/db.js"]:::server

        subgraph "Routing Layer"
            direction TB
            AuthRoutes["routes/authRoutes.js"]:::routes
            UserRoutes["routes/userRoutes.js"]:::routes
            BlogRoutes["routes/blogRoutes.js"]:::routes
        end

        AuthMiddleware["middlewares/authMiddlewares.js"]:::middleware

        subgraph "Controllers"
            direction TB
            AuthController["controllers/authControllers.js"]:::controllers
            UserController["controllers/usercontroller.js"]:::controllers
            BlogController["controllers/blogcontroller.js"]:::controllers
        end

        subgraph "Models"
            direction TB
            UserModel["models/user.js"]:::models
            BlogModel["models/blog.js"]:::models
        end

        Utils["utils/jwt.js"]:::utils
    end

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

    %% Data Flows
    Client -->|"HTTP Requests with JWT"| Index
    Index -->|"load DB connection"| DBConn
    DBConn -->|connects to| MongoDB

    Index --> AuthRoutes
    Index --> UserRoutes
    Index --> BlogRoutes

    AuthRoutes -->|"POST /login, POST /register"| AuthController
    AuthRoutes --> AuthMiddleware
    UserRoutes -->|"Protected"| AuthMiddleware
    BlogRoutes -->|"Protected"| AuthMiddleware

    AuthMiddleware --> Controllers

    AuthController --> UserModel
    AuthController --> Utils
    UserController --> UserModel
    BlogController --> BlogModel

    UserModel --> MongoDB
    BlogModel --> MongoDB

    Utils -->|"JWT sign/verify"| AuthController

    %% Click Events
    click Index "https://github.com/yogeshawasthi/blog_backend_in_mern/blob/main/Index.js"
    click DBConn "https://github.com/yogeshawasthi/blog_backend_in_mern/blob/main/config/db.js"
    click AuthRoutes "https://github.com/yogeshawasthi/blog_backend_in_mern/blob/main/routes/authRoutes.js"
    click UserRoutes "https://github.com/yogeshawasthi/blog_backend_in_mern/blob/main/routes/userRoutes.js"
    click BlogRoutes "https://github.com/yogeshawasthi/blog_backend_in_mern/blob/main/routes/blogRoutes.js"
    click AuthMiddleware "https://github.com/yogeshawasthi/blog_backend_in_mern/blob/main/middlewares/authMiddlewares.js"
    click AuthController "https://github.com/yogeshawasthi/blog_backend_in_mern/blob/main/controllers/authControllers.js"
    click UserController "https://github.com/yogeshawasthi/blog_backend_in_mern/blob/main/controllers/usercontroller.js"
    click BlogController "https://github.com/yogeshawasthi/blog_backend_in_mern/blob/main/controllers/blogcontroller.js"
    click UserModel "https://github.com/yogeshawasthi/blog_backend_in_mern/blob/main/models/user.js"
    click BlogModel "https://github.com/yogeshawasthi/blog_backend_in_mern/blob/main/models/blog.js"
    click Utils "https://github.com/yogeshawasthi/blog_backend_in_mern/blob/main/utils/jwt.js"

    %% Styles
    classDef client fill:#f9f,stroke:#333,stroke-width:1px;
    classDef server fill:#bbf,stroke:#333,stroke-width:1px;
    classDef routes fill:#8bf,stroke:#333,stroke-width:1px;
    classDef middleware fill:#fb8,stroke:#333,stroke-width:1px;
    classDef controllers fill:#8ff,stroke:#333,stroke-width:1px;
    classDef models fill:#bfb,stroke:#333,stroke-width:1px;
    classDef utils fill:#fbf,stroke:#333,stroke-width:1px;
    classDef db fill:#ffb,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5;
```
