# yousaf184/notely architecture

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

- Interactive diagram: https://gitdiagram.com/yousaf184/notely
- Repository: https://github.com/yousaf184/notely

## Overview

##### Main Components
- Client (Browser): This layer includes all the static assets served from the public folder – the CSS files (e.g., auth.css, home.css, etc.) and client-side JavaScript (e.g., auth.js, home.js). It also renders the HTML pages produced by the server.
- Server (Express Application): This is the heart of the system. It includes:
– Entry Points: The “src/index.js” and “src/app.js” files start up the Express server.
– Routes: Located in the “src/routes” directory (auth.route.js and notes.route.js) to define request endpoints.
– Controllers: In “src/controllers”, these handle business logic for authentication (auth.controller.js) and note management (notes.controller.js).
– Middlewares: From “src/middlewares” (e.g., isLoggedIn and error middleware) for tasks such as authentication checking and error handling.
– Models: In “src/models”, representing the Note and User data structures.
– Utilities and Helpers: From “src/utils”, which assist with authentication, note-related operations, constant values, and response formatting.
– Database Layer: The “src/lib/db.js” file is responsible for initializing the connection to MongoDB.
– Views: EJS templates under “src/views” for rendering pages (organized into pages and partials).

##### Relationships and Interactions
- Data Flow from Client to the Backend:
– A user accesses the application via a browser and sends HTTP requests (e.g., for viewing, creating, or editing notes).
– The Express app first intercepts these requests through its routing layer.
– Appropriate middlewares (such as authentication checking) get executed before handing off to controllers.
- Controller and Model Interaction:
– Controllers process a request by invoking business logic, util functions, and querying models that interact with MongoDB via the db.js connection.
- Response Generation:
– After processing, controllers pass data to view templates (EJS) to generate HTML, which is then sent back as the response.
- Error Handling:
– If an error occurs at any step, the error.middleware captures it and directs it to an error view (error.ejs).

##### Architectural Patterns and Important Design Principles
- MVC (Model-View-Controller): The application strictly separates concerns by dividing responsibilities among routes, controllers, models, and views.
- Middleware-Oriented Design: Custom middlewares (like authentication and error handling) are used to process requests systematically.
- Modular Structure: The project is broken down into clear modules (controllers, models, routes, etc.), making it easier to scale and maintain.
- Environment Configuration: The app leverages environment variables (as indicated by the .env instructions in the README) to manage configuration details like PORT, NODE_ENV, and DATABASE_URI.

##### Technologies and Frameworks
- Node.js with Express as the web server framework.
- EJS as the templating engine for dynamic HTML rendering.
- MongoDB (connected via the URI provided in the environment variables).
- JWT for handling authentication (as deduced from JWT_SECRET_KEY in the README).
- Client-side JavaScript and CSS for enhancing front-end interactions.

- “Client Browser” displaying static assets and EJS-rendered HTML.
- “Express Server” subdivided into key sections: Routes, Controllers, Middlewares, Models/Database Connection, and Utilities.
- “Database” indicating MongoDB.
- Data flow from the client to the server and vice versa.
- Dependencies among routes, controllers, models, and middlewares.
- UI components (Client and public assets).
- Business logic components (Controllers, Middlewares).
- Data management components (Models, Database).
- Annotations to describe the purpose of environmental configurations (such as PORT, JWT_SECRET_KEY, DATABASE_URI) that influence application behavior.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Client Layer
    subgraph "Client Layer"
        B("Client Browser"):::client
        P("Static Assets"):::client
    end

    %% Server Layer
    subgraph "Server Layer"
        %% Server Startup
        subgraph "Server Startup"
            I("index.js"):::server
            A("app.js"):::server
        end

        %% Routes
        subgraph "Routes"
            AR("auth.route.js"):::routes
            NR("notes.route.js"):::routes
        end

        %% Middlewares
        subgraph "Middlewares"
            EM("error.middleware.js"):::middleware
            LM("isLoggedIn.middleware.js"):::middleware
        end

        %% Controllers
        subgraph "Controllers"
            AC("auth.controller.js"):::controller
            NC("notes.controller.js"):::controller
        end

        %% Models
        subgraph "Models"
            NM("note.model.js"):::model
            UM("user.model.js"):::model
        end

        %% Database
        subgraph "Database"
            DB("db.js"):::database
        end

        %% Utilities and Helpers
        subgraph "Utilities"
            AU("authUtils.js"):::utility
            CO("constants.js"):::utility
            NU("notesUtils.js"):::utility
            RU("responseUtils.js"):::utility
        end

        %% Views
        subgraph "Views"
            AE("auth.ejs"):::view
            HO("home.ejs"):::view
            EE("error.ejs"):::view
            NE("newOrEditNote.ejs"):::view
            ONE("note.ejs"):::view
            PA("Partials"):::view
        end
    end

    %% Connections from Client to Server Startup
    B -->|"HTTP_Request"| I
    B -->|"Loads_Assets"| P

    %% Connections from Server Startup to Routes
    I -->|"Initializes"| AR
    I -->|"Initializes"| NR
    A -->|"Initializes"| AR
    A -->|"Initializes"| NR

    %% Routes to Middlewares and Controllers
    AR -->|"UsesAuth"| LM
    NR -->|"UsesAuth"| LM

    AR -->|"Delegates"| AC
    NR -->|"Delegates"| NC

    %% Middlewares error handling connection to Views
    EM -->|"HandlesError"| EE

    %% Controllers interactions with Middlewares, Utilities, Models and Views
    LM -->|"Authorizes"| AC
    AC -->|"Utilizes"| AU
    AC -->|"Utilizes"| CO
    AC -->|"Utilizes"| NU
    AC -->|"Utilizes"| RU

    NC -->|"Utilizes"| AU
    NC -->|"Utilizes"| CO
    NC -->|"Utilizes"| NU
    NC -->|"Utilizes"| RU

    AC -->|"DataAccess"| NM
    AC -->|"DataAccess"| UM
    NC -->|"DataAccess"| NM
    NC -->|"DataAccess"| UM

    AC -->|"Renders"| AE
    AC -->|"Renders"| HO
    NC -->|"Renders"| NE
    NC -->|"Renders"| ONE

    %% Models to Database
    NM -->|"Queries"| DB
    UM -->|"Queries"| DB

    %% Views include shared partials
    AE -->|"Includes"| PA
    HO -->|"Includes"| PA
    NE -->|"Includes"| PA
    ONE -->|"Includes"| PA

    %% Responses back to Client Browser
    AE -->|"HTML_Response"| B
    HO -->|"HTML_Response"| B
    NE -->|"HTML_Response"| B
    ONE -->|"HTML_Response"| B
    EE -->|"HTML_Response"| B

    %% Click Events for mapped components
    click P "https://github.com/yousaf184/notely/tree/main/public/"
    click I "https://github.com/yousaf184/notely/blob/main/src/index.js"
    click A "https://github.com/yousaf184/notely/blob/main/src/app.js"
    click AR "https://github.com/yousaf184/notely/blob/main/src/routes/auth.route.js"
    click NR "https://github.com/yousaf184/notely/blob/main/src/routes/notes.route.js"
    click AC "https://github.com/yousaf184/notely/blob/main/src/controllers/auth.controller.js"
    click NC "https://github.com/yousaf184/notely/blob/main/src/controllers/notes.controller.js"
    click LM "https://github.com/yousaf184/notely/blob/main/src/middlewares/isLoggedIn.middleware.js"
    click EM "https://github.com/yousaf184/notely/blob/main/src/middlewares/error.middleware.js"
    click NM "https://github.com/yousaf184/notely/blob/main/src/models/note.model.js"
    click UM "https://github.com/yousaf184/notely/blob/main/src/models/user.model.js"
    click DB "https://github.com/yousaf184/notely/blob/main/src/lib/db.js"
    click AU "https://github.com/yousaf184/notely/blob/main/src/utils/authUtils.js"
    click CO "https://github.com/yousaf184/notely/blob/main/src/utils/constants.js"
    click NU "https://github.com/yousaf184/notely/blob/main/src/utils/notesUtils.js"
    click RU "https://github.com/yousaf184/notely/blob/main/src/utils/responseUtils.js"
    click AE "https://github.com/yousaf184/notely/blob/main/src/views/auth.ejs"
    click HO "https://github.com/yousaf184/notely/blob/main/src/views/home.ejs"
    click EE "https://github.com/yousaf184/notely/blob/main/src/views/error.ejs"
    click NE "https://github.com/yousaf184/notely/blob/main/src/views/newOrEditNote.ejs"
    click ONE "https://github.com/yousaf184/notely/blob/main/src/views/note.ejs"
    click PA "https://github.com/yousaf184/notely/tree/main/src/views/partials"

    %% Class Definitions for styles
    classDef client fill:#b3e5fc,stroke:#01579b,stroke-width:2px;
    classDef server fill:#bbdefb,stroke:#0d47a1,stroke-width:2px;
    classDef routes fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px;
    classDef middleware fill:#ffcdd2,stroke:#c62828,stroke-width:2px;
    classDef controller fill:#d1c4e9,stroke:#512da8,stroke-width:2px;
    classDef model fill:#fff9c4,stroke:#f9a825,stroke-width:2px;
    classDef database fill:#ffe0b2,stroke:#ef6c00,stroke-width:2px;
    classDef utility fill:#d7ccc8,stroke:#4e342e,stroke-width:2px;
    classDef view fill:#f0f4c3,stroke:#afb42b,stroke-width:2px;
```
