# hieu-le296/smalltalks-server architecture

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

- Interactive diagram: https://gitdiagram.com/hieu-le296/smalltalks-server
- Repository: https://github.com/hieu-le296/smalltalks-server

## Overview

- API Server (Express)
- Central Express.js application, launched by server.js. This is the entry point for all incoming requests.
- Routes
- Routes directory with endpoints (auth, posts, comments, users, contactMe, routeStats). These file mappings should be connected to the Controllers.
- Controllers
- Route file to its corresponding controller (e.g., controllers/auth.js, controllers/comments.js), where business logic is implemented.
- Middleware
- Middleware components (async handling, error handling, authentication, advanced filters, URL detection, validation) that intercept requests before they hit the controllers.
- Models
- Data models (User, Post, Comment, RouteStat) which encapsulate the database schemas and serve as the ORM layer.
- Database and Data Seeder
- Interaction with the database (represented by DB.sql and the configuration in config/db.js). Also include seeder.js as the component that loads or cleans up mockup data (_data directory containing JSON files).
- Static Public Files
- “public” folder which contains static files like index.html, 404.html, and directories for uploads. Though not the core API, they form part of the serving layer.
- Configuration and Utilities
- The config folder (with environment variables and database config setups) and the utils folder include functions for image processing, email sending, error throwing, and custom database queries. These provide supporting functionality and should be shown as cross-cutting concerns.

##### Relationships and Data Flow
- Clients (e.g., front-end applications or API consumers) send HTTP requests to the API Server.
- Once pre-processing is complete, the request is routed to the appropriate route handler which directs it to a specific controller.
- The controller handles business logic and interacts with the Models to perform CRUD operations on the database.
- Utility functions (e.g., ImageHandler.js, sendEmail.js) are called as needed within controllers to perform auxiliary operations.
- Finally, responses (or errors) are routed back through middleware (e.g., error.js) before being sent back to the client.

##### Architectural Patterns and Design Principles
- MVC (or a variant) pattern: Separation of routing, business logic (controllers), and data models.
- Modular architecture: Independent middleware, controllers, and models enhance maintainability.
- Environment-based configuration: Centralized in the config folder to ease deployments in various environments.
- Clear error handling and asynchronous processing: Provided by dedicated middleware (async.js, error.js).

##### Essential Elements
- On a subsequent level, group the Routes and Controllers, showing the mapping between specific endpoints and business logic.
- Models as the data layer interacting with the database (with DB.sql, config/db.js connected).
- Supporting utilities (ImageHandler, sendEmail, etc.) as side modules that interact with controllers.

Happy diagramming!

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    Client["Client (Browser/API Consumer)"]:::client

    API["API Server (Express)"]:::api

    %% Subgraph: Middleware
    subgraph "Middleware Layer"
        m1["async.js"]:::middleware
        m2["error.js"]:::middleware
        m3["auth.js"]:::middleware
        m4["advancedFilters.js"]:::middleware
        m5["routeURLDetector.js"]:::middleware
        m6["validation.js"]:::middleware
    end

    %% Subgraph: Routes
    subgraph "Routes"
        r1["auth.js"]:::routes
        r2["comments.js"]:::routes
        r3["contactMe.js"]:::routes
        r4["posts.js"]:::routes
        r5["routeStats.js"]:::routes
        r6["users.js"]:::routes
    end

    %% Subgraph: Controllers
    subgraph "Controllers"
        c1["auth.js"]:::controllers
        c2["comments.js"]:::controllers
        c3["contactMe.js"]:::controllers
        c4["posts.js"]:::controllers
        c5["routeStats.js"]:::controllers
        c6["users.js"]:::controllers
    end

    %% Subgraph: Models
    subgraph "Models"
        mod1["User.js"]:::models
        mod2["Post.js"]:::models
        mod3["Comment.js"]:::models
        mod4["RouteStat.js"]:::models
    end

    %% Subgraph: Database & Seeder
    subgraph "Database & Seeder"
        db1["DB.sql"]:::database
        db2["db.js"]:::database
        db3["seeder.js"]:::database
        db4["_data/ JSON Files"]:::database
    end

    %% Subgraph: Static Public Files
    subgraph "Static Public Files"
        pub1["index.html"]:::public
        pub2["404.html"]:::public
        pub3["uploads Directory"]:::public
    end

    %% Subgraph: Configuration & Utilities
    subgraph "Configuration & Utilities"
        cu1["config.env"]:::config
        CU_agg["Config & Utilities"]:::config
        cu2["ImageHandler.js"]:::utils
        cu3["db_query.js"]:::utils
        cu4["sendEmail.js"]:::utils
        cu5["throwError.js"]:::utils
    end

    %% Define Relationships and Data Flow
    Client -->|"HTTP Request"| API
    API -->|"Init Middleware"| m1

    %% Chain Middleware functions
    m1 --> m2
    m2 --> m3
    m3 --> m4
    m4 --> m5
    m5 --> m6

    %% Route mapping: Middleware to Routes (each endpoint is reached after all middleware)
    m6 -- "routes" --> r1
    m6 -- "routes" --> r2
    m6 -- "routes" --> r3
    m6 -- "routes" --> r4
    m6 -- "routes" --> r5
    m6 -- "routes" --> r6

    %% Routes to Controllers mapping
    r1 -->|"executes"| c1
    r2 -->|"executes"| c2
    r3 -->|"executes"| c3
    r4 -->|"executes"| c4
    r5 -->|"executes"| c5
    r6 -->|"executes"| c6

    %% Controllers interact with Models
    c1 -->|"CRUD"| mod1
    c2 -->|"CRUD"| mod3
    c3 -->|"CRUD"| mod1
    c4 -->|"CRUD"| mod2
    c5 -->|"CRUD"| mod4
    c6 -->|"CRUD"| mod1

    %% Models communicate with the Database
    mod1 -->|"query"| db1
    mod2 -->|"query"| db1
    mod3 -->|"query"| db1
    mod4 -->|"query"| db1
    db1 --> db2
    db2 --> db3
    db3 --> db4

    %% API Server also serves static files
    API -- "serves" --> pub1
    API -- "serves" --> pub2
    API -- "serves" --> pub3

    %% Controllers call Configuration & Utilities functions
    c4 -- "calls" --> CU_agg
    CU_agg --> cu1
    CU_agg --> cu2
    CU_agg --> cu3
    CU_agg --> cu4
    CU_agg --> cu5

    %% Click Events
    click API "https://github.com/hieu-le296/smalltalks-server/blob/main/server.js"
    click r1 "https://github.com/hieu-le296/smalltalks-server/blob/main/routes/auth.js"
    click r2 "https://github.com/hieu-le296/smalltalks-server/blob/main/routes/comments.js"
    click r3 "https://github.com/hieu-le296/smalltalks-server/blob/main/routes/contactMe.js"
    click r4 "https://github.com/hieu-le296/smalltalks-server/blob/main/routes/posts.js"
    click r5 "https://github.com/hieu-le296/smalltalks-server/blob/main/routes/routeStats.js"
    click r6 "https://github.com/hieu-le296/smalltalks-server/blob/main/routes/users.js"
    click c1 "https://github.com/hieu-le296/smalltalks-server/blob/main/controllers/auth.js"
    click c2 "https://github.com/hieu-le296/smalltalks-server/blob/main/controllers/comments.js"
    click c3 "https://github.com/hieu-le296/smalltalks-server/blob/main/controllers/contactMe.js"
    click c4 "https://github.com/hieu-le296/smalltalks-server/blob/main/controllers/posts.js"
    click c5 "https://github.com/hieu-le296/smalltalks-server/blob/main/controllers/routeStats.js"
    click c6 "https://github.com/hieu-le296/smalltalks-server/blob/main/controllers/users.js"
    click m1 "https://github.com/hieu-le296/smalltalks-server/blob/main/middleware/async.js"
    click m2 "https://github.com/hieu-le296/smalltalks-server/blob/main/middleware/error.js"
    click m3 "https://github.com/hieu-le296/smalltalks-server/blob/main/middleware/auth.js"
    click m4 "https://github.com/hieu-le296/smalltalks-server/blob/main/middleware/advancedFilters.js"
    click m5 "https://github.com/hieu-le296/smalltalks-server/blob/main/middleware/routeURLDetector.js"
    click m6 "https://github.com/hieu-le296/smalltalks-server/blob/main/middleware/validation.js"
    click mod1 "https://github.com/hieu-le296/smalltalks-server/blob/main/models/User.js"
    click mod2 "https://github.com/hieu-le296/smalltalks-server/blob/main/models/Post.js"
    click mod3 "https://github.com/hieu-le296/smalltalks-server/blob/main/models/Comment.js"
    click mod4 "https://github.com/hieu-le296/smalltalks-server/blob/main/models/RouteStat.js"
    click db1 "https://github.com/hieu-le296/smalltalks-server/blob/main/DB.sql"
    click db2 "https://github.com/hieu-le296/smalltalks-server/blob/main/config/db.js"
    click db3 "https://github.com/hieu-le296/smalltalks-server/blob/main/seeder.js"
    click db4 "https://github.com/hieu-le296/smalltalks-server/tree/main/_data/"
    click pub1 "https://github.com/hieu-le296/smalltalks-server/blob/main/public/index.html"
    click pub2 "https://github.com/hieu-le296/smalltalks-server/blob/main/public/404.html"
    click pub3 "https://github.com/hieu-le296/smalltalks-server/tree/main/public/uploads"
    click cu1 "https://github.com/hieu-le296/smalltalks-server/blob/main/config/config.env"
    click cu2 "https://github.com/hieu-le296/smalltalks-server/blob/main/utils/ImageHandler.js"
    click cu3 "https://github.com/hieu-le296/smalltalks-server/blob/main/utils/db_query.js"
    click cu4 "https://github.com/hieu-le296/smalltalks-server/blob/main/utils/sendEmail.js"
    click cu5 "https://github.com/hieu-le296/smalltalks-server/blob/main/utils/throwError.js"

    %% Styles
    classDef client fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef api fill:#d1e7dd,stroke:#333,stroke-width:2px;
    classDef middleware fill:#f8d7da,stroke:#333,stroke-width:2px;
    classDef routes fill:#fff3cd,stroke:#333,stroke-width:2px;
    classDef controllers fill:#e2e3e5,stroke:#333,stroke-width:2px;
    classDef models fill:#d1c4e9,stroke:#333,stroke-width:2px;
    classDef database fill:#f0e68c,stroke:#333,stroke-width:2px;
    classDef public fill:#ffeeba,stroke:#333,stroke-width:2px;
    classDef config fill:#cfd8dc,stroke:#333,stroke-width:2px;
    classDef utils fill:#b3e5fc,stroke:#333,stroke-width:2px;
```
