# hien2703g/backend architecture

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

- Interactive diagram: https://gitdiagram.com/hien2703g/backend
- Repository: https://github.com/hien2703g/backend

## Overview

##### Project Type and Purpose
- This is a full-stack Node.js/Express “product-management” (e-commerce style) application with two user-facing frontends (Admin and Client), a REST-style backend, real-time chat, file uploads, email notifications, and MongoDB as the data store. It follows an MVC pattern with clear separation between routes, controllers, models, views, and helper services.

##### Main Architectural Components
A. Browser Frontends (Clients)
- Admin UI: Pug templates under views/admin + static assets (public/Admin).
- Client UI: Pug templates under views/client + static assets (public/Client).
- Socket.io client (chat.js, socket.js) for real-time chat.

B. Node.js Backend
- Entry Point: index.js (creates Express app, HTTP server, attaches Socket.io).
- Routes Layer:
- routes/admin/** – Admin API endpoints
- routes/client/** – Client API endpoints
- Controllers Layer:
- controllers/admin/** – Business logic for admin features (accounts, products, orders, settings, roles, dashboard)
- controllers/client/** – Business logic for client-side features (home, cart, checkout, chat, user, search)
- Middleware Layer:
- Common middleware: body-parser, method-override, cookie/session handling, flash messages
- Admin-specific: auth, settings, file upload to Cloudinary
- Client-specific: auth, cart, category, setting, file upload
- Models Layer:
- Mongoose schemas & models (account, user, role, product, category, cart, order, chat, settings, forgot-password)
- Helper/Service Layer (utility modules in Helper/):
- createTree, pagination, filterStatus, search, email sending (nodemailer), file storage (multer + Cloudinary), slug generation, formatting

C. Data Store
- Primary: MongoDB (connected via Mongoose in config/database.js)
- Seed data JSON: Products_shop.*.json, products-test.*.json

D. External Services
- Cloudinary (images), SMTP (Gmail via nodemailer), Socket.io (real-time chat), possibly third-party libs (moment, express-flash, emoji-picker, file-upload-with-preview, viewerjs)

##### Relationships and Data Flows
1. Browser → HTTP(S) → Express Routes
2. Routes → Middlewares (auth, upload, validation)
3. Middlewares → Controllers → Helpers/Services as needed
4. Controllers → Models → MongoDB (CRUD operations)
5. Controllers → External Services:
- File upload flows to Cloudinary
- Email flows via nodemailer to SMTP
- Real-time flows via Socket.io channels
6. Responses → Pug views rendered or JSON sent back → Browser

##### Architectural Patterns & Design Principles
- MVC: clear layering
- Modularization: routes/controllers/models split by feature and by “admin” vs. “client”
- Separation of concerns: helper services for cross-cutting tasks (pagination, file storage, email)
- Real-time event-driven architecture for chat (socket.io) separate from REST flows
- Environment/config management in config/system.js

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Actors
    AdminBrowser["Admin Browser"]:::frontend
    ClientBrowser["Client Browser"]:::frontend

    %% Frontend UIs
    AdminUI["Admin UI<br/>(Pug templates & static assets)"]:::frontend
    ClientUI["Client UI<br/>(Pug templates & static assets)"]:::frontend
    SocketClient["Socket.io Client<br/>(chat.js, socket.js)"]:::frontend

    %% Web Application Container
    subgraph "Web Application (Node.js/Express)" 
        direction TB
        EntryPoint["index.js"]:::backend
        subgraph "Routes Layer"
            direction TB
            AdminRoutes["Admin Routes<br/>(/admin/*)"]:::backend
            ClientRoutes["Client Routes<br/>(/client/*)"]:::backend
        end
        subgraph "Middleware Layer"
            direction TB
            CommonMiddleware["Common Middleware"]:::backend
            AdminMiddleware["Admin Middleware"]:::backend
            ClientMiddleware["Client Middleware"]:::backend
        end
        subgraph "Controllers Layer"
            direction TB
            AdminControllers["Admin Controllers"]:::backend
            ClientControllers["Client Controllers"]:::backend
        end
        ModelsLayer["Models (Mongoose)"]:::backend
        Helpers["Helper Services"]:::backend
        SocketServer["Socket.io Server"]:::backend
    end

    %% Data Store
    MongoDB[(MongoDB)]:::external

    %% External Services
    Cloudinary((Cloudinary)):::external
    SMTP((SMTP via Nodemailer)):::external

    %% Connections
    AdminBrowser -->|"HTTP /admin/*"| AdminRoutes
    ClientBrowser -->|"HTTP /client/*"| ClientRoutes
    AdminBrowser -->|"Socket.io"| SocketServer
    ClientBrowser -->|"Socket.io"| SocketServer

    AdminUI --> AdminBrowser
    ClientUI --> ClientBrowser
    SocketClient --> ClientBrowser

    EntryPoint --> AdminRoutes
    EntryPoint --> ClientRoutes
    EntryPoint --> SocketServer

    AdminRoutes --> CommonMiddleware
    ClientRoutes --> CommonMiddleware
    AdminRoutes --> AdminMiddleware
    ClientRoutes --> ClientMiddleware

    CommonMiddleware --> AdminControllers
    CommonMiddleware --> ClientControllers
    AdminMiddleware --> AdminControllers
    ClientMiddleware --> ClientControllers

    AdminControllers --> ModelsLayer
    ClientControllers --> ModelsLayer
    AdminControllers --> Helpers
    ClientControllers --> Helpers

    Helpers --> Cloudinary
    Helpers --> SMTP

    ModelsLayer --> MongoDB

    %% Click Events
    click AdminUI "https://github.com/hien2703g/backend/tree/main/views/admin/"
    click AdminUI "https://github.com/hien2703g/backend/tree/main/public/Admin/"
    click ClientUI "https://github.com/hien2703g/backend/tree/main/views/client/"
    click ClientUI "https://github.com/hien2703g/backend/tree/main/public/Client/"
    click SocketClient "https://github.com/hien2703g/backend/blob/main/public/Client/Js/socket.js"
    click SocketClient "https://github.com/hien2703g/backend/blob/main/public/Client/Js/chat.js"
    click EntryPoint "https://github.com/hien2703g/backend/blob/main/index.js"
    click AdminRoutes "https://github.com/hien2703g/backend/tree/main/routes/admin/"
    click ClientRoutes "https://github.com/hien2703g/backend/tree/main/routes/client/"
    click AdminControllers "https://github.com/hien2703g/backend/tree/main/controllers/admin/"
    click ClientControllers "https://github.com/hien2703g/backend/tree/main/controllers/client/"
    click AdminMiddleware "https://github.com/hien2703g/backend/tree/main/middlewares/admin/"
    click ClientMiddleware "https://github.com/hien2703g/backend/tree/main/middlewares/client/"
    click ModelsLayer "https://github.com/hien2703g/backend/tree/main/models/"
    click CreateTree "https://github.com/hien2703g/backend/blob/main/Helper/createTree.js"
    click FilterStatus "https://github.com/hien2703g/backend/blob/main/Helper/filterStatus.js"
    click Format "https://github.com/hien2703g/backend/blob/main/Helper/format.js"
    click Generate "https://github.com/hien2703g/backend/blob/main/Helper/generate.js"
    click Pagination "https://github.com/hien2703g/backend/blob/main/Helper/pagination.js"
    click ProductCategory "https://github.com/hien2703g/backend/blob/main/Helper/product-category.js"
    click ProductHelper "https://github.com/hien2703g/backend/blob/main/Helper/product.js"
    click Search "https://github.com/hien2703g/backend/blob/main/Helper/search.js"
    click SendMail "https://github.com/hien2703g/backend/blob/main/Helper/send-mail.js"
    click StorageMulter "https://github.com/hien2703g/backend/blob/main/Helper/storageMulter.js"
    click UploadToCloudinary "https://github.com/hien2703g/backend/blob/main/Helper/uploadToCloudinary.js"

    %% Styles
    classDef frontend fill:#e0f7da,stroke:#2e7d32,color:#2e7d32
    classDef backend fill:#e3f2fd,stroke:#1565c0,color:#1565c0
    classDef external fill:#fff3e0,stroke:#ef6c00,color:#ef6c00
```
