# linhinthehood/projectnodejs architecture

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

- Interactive diagram: https://gitdiagram.com/linhinthehood/projectnodejs
- Repository: https://github.com/linhinthehood/projectnodejs

## Overview

##### Main System Components
- Backend Server: The core of the application, driven by Express (server.js), handling requests through defined routes (located in the routes folder) for authentication, products (shirts, bottoms, outerwear, accessories), profile management, about us, and orders.
- Database: MongoDB Atlas is used as the primary datastore, with GridFS for file storage ("assets" content and other static files), and models defined for products, receipts, and users.
- Frontend & View Layer: Uses the EJS template engine (views folder) to render the web pages. Static assets and public files (e.g., public/load-balancer-demo.html) are served from designated directories.
- Message Broker: RabbitMQ is integrated for handling asynchronous operations (as seen by configurations in config/rabbitmq.js and usage in services like emailService.js and messageService.js). This component is critical for decoupling tasks such as sending welcome emails or order confirmation notifications.
- Worker Processes: Separate processes (worker.js and workers/messagingWorkers.js) are dedicated to processing queued tasks/messages, ensuring that non-blocking operations do not interfere with the main application flow.
- Caching & Session Management: Redis is used for managing sessions (as indicated by packages like connect-redis and configuration for session-based authentication).
- Load Balancer/Reverse Proxy: Nginx is configured (nginx.conf) to act as the load balancer to distribute incoming requests, and the Docker setup (Dockerfile and docker-compose.yml) ensures containerized deployments.

- The MongoDB database for data persistence.
- The RabbitMQ Message Broker for inter-service communication.
- Redis for session storage.
- The EJS templating engine (frontend views) to generate dynamic HTML pages.
- A separate container/block for “Worker Processes” that subscribes to the RabbitMQ queues to process asynchronous tasks.
- Nginx as a front-facing component that handles load balancing before forwarding traffic to the main application server.
- Client requests entering via Nginx.
- API calls from the Express Server to MongoDB, Redis, and RabbitMQ.
- RabbitMQ dispatching messages to the worker processes.
- The response flow from the server back to the client.
- Other minor external dependencies as needed (e.g., third-party email services if referenced through emailService.js).

##### Architectural Patterns and Design Principles
- Employ a microservice-inspired design even within the monolith by decoupling asynchronous tasks using message queues.
- Clearly delineate between synchronous operations (user interactions, data retrieval) and asynchronous operations (notifications, order confirmations) for improved scalability and fault tolerance.
- That Docker containers encapsulate the application, load balancer (Nginx), and supporting services (Redis, RabbitMQ), ensuring environment consistency.

- Docker-based infrastructure, as the Dockerfile and docker-compose.yml are crucial for orchestrating the different components (including Nginx for load balancing and containerized backend services).

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["Client Browser"]:::client

    subgraph "Docker Containers"
        B["Nginx Load Balancer"]:::lb

        subgraph "Backend (Express Application Server)"
            C["Express Application Server"]:::app
            C1["Routes"]:::app
            C2["Controllers"]:::app
            C3["Middlewares"]:::app
        end

        subgraph "MongoDB & GridFS"
            D1["DB Config"]:::db
            D2["Product Model"]:::db
            D3["Receipt Model"]:::db
            D4["User Model"]:::db
        end

        E["Redis Cache"]:::db

        subgraph "RabbitMQ Broker"
            F1["RabbitMQ Config"]:::mq
            F2["Email Service"]:::mq
            F3["Message Service"]:::mq
        end

        subgraph "Worker Processes"
            H1["Main Worker"]:::worker
            H2["Messaging Workers"]:::worker
        end
    end

    subgraph "Frontend"
        G1["EJS Templates"]:::frontend
        G2["Static Assets"]:::frontend
    end

    I["Docker Containers (Orchestration)"]:::docker

    %% Main Data Flow
    A -->|"Request"| B
    B -->|"Forwards"| C
    C -->|"Manages"| C1
    C -->|"Manages"| C2
    C -->|"Manages"| C3
    C -->|"DataOps"| D1
    C -->|"Session"| E
    C -->|"Publishes"| F1
    F1 -->|"Dispatches"| H1
    F1 -->|"Dispatches"| H2
    C -->|"Renders"| G1
    G1 -->|"Serves"| G2
    G1 -->|"Response"| A

    %% Docker Orchestration Connections (dashed)
    I --- B
    I --- C
    I --- D1
    I --- E
    I --- F1
    I --- H1

    %% Click Events for Component Mapping
    click C "https://github.com/linhinthehood/projectnodejs/blob/main/server.js"
    click C1 "https://github.com/linhinthehood/projectnodejs/tree/main/routes"
    click C2 "https://github.com/linhinthehood/projectnodejs/tree/main/controllers"
    click C3 "https://github.com/linhinthehood/projectnodejs/tree/main/middlewares"
    click D1 "https://github.com/linhinthehood/projectnodejs/blob/main/config/db.js"
    click D2 "https://github.com/linhinthehood/projectnodejs/blob/main/models/productModel.js"
    click D3 "https://github.com/linhinthehood/projectnodejs/blob/main/models/receiptModel.js"
    click D4 "https://github.com/linhinthehood/projectnodejs/blob/main/models/userModel.js"
    click G1 "https://github.com/linhinthehood/projectnodejs/tree/main/views"
    click G2 "https://github.com/linhinthehood/projectnodejs/tree/main/public"
    click F1 "https://github.com/linhinthehood/projectnodejs/blob/main/config/rabbitmq.js"
    click F2 "https://github.com/linhinthehood/projectnodejs/blob/main/services/emailService.js"
    click F3 "https://github.com/linhinthehood/projectnodejs/blob/main/services/messageService.js"
    click H1 "https://github.com/linhinthehood/projectnodejs/blob/main/worker.js"
    click H2 "https://github.com/linhinthehood/projectnodejs/blob/main/workers/messagingWorkers.js"
    click B "https://github.com/linhinthehood/projectnodejs/blob/main/nginx.conf"
    click I "https://github.com/linhinthehood/projectnodejs/tree/main/Dockerfile"
    click I "https://github.com/linhinthehood/projectnodejs/blob/main/docker-compose.yml"

    %% Style Definitions
    classDef app fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef db fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef mq fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef worker fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef lb fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef frontend fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
    classDef client fill:#fefefe,stroke:#333,stroke-width:2px;
    classDef docker fill:#d1c4e9,stroke:#5e35b1,stroke-width:2px;
```
