# markkhramko/nodejs-express-jwt architecture

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

- Interactive diagram: https://gitdiagram.com/markkhramko/nodejs-express-jwt
- Repository: https://github.com/markkhramko/nodejs-express-jwt

## Overview

##### Main Components
- Server Entry Point: Indicate app/app.js as the central bootstrapping file.
- Controllers: Illustrate two sets – API controllers (in app/controllers/api) responsible for REST endpoints and Web controllers (in app/controllers/web) for HTTP request handling and view rendering.
- Routes: Show how the routes (located in app/routes, with separate directories for API and web) are mapped to the respective controllers.
- Models: Represent the database models (in app/models), which are defined with Sequelize. Connect these to the corresponding services and the database.
- db.service.js for database interactions,
- bcrypt.service.js for password hashing,
- jwt.service.js for token management.
- Facades & Factories: Indicate the error handling (under app/factories/errors) and response factories (app/factories/responses) which help standardize responses.
- Policies: Include the policies (in app/policies) that serve as middleware enforcing authentication and authorization rules.
- Configurations: Represent the configs directory (with separate files for API, database, environments, JWT, and server settings) alongside .env for environment values.
- Migrations and Seeders: Highlight the migrator folder for database schema migrations and seed data processes.
- Public & Views: Show the public directory for static assets and app/views for Pug templates used in web responses.
- Utilities: Optionally, mark additional helper modules such as those under app/utils.

##### Relationships and Interactions
- From controllers, indicate interconnections with models (database queries via Sequelize) and services (e.g., calling JWT or bcrypt functions).
- Link policies as middleware between the request routing and controller actions.
- That the configurations are loaded first and influence all components (server, JWT, DB, etc.).
- Database (MySQL/PostgreSQL) being accessed by models through the db.service.
- Optionally, indicate the role played by factories and facades in error and response management.

##### Architectural Patterns
- Separation of concerns: distinct modules for request routing, business logic (controllers), data models, and service utilities.
- Use of the factory pattern (seen in controllers and response/error factories) to create consistent layers of abstraction.
- Middleware (via policies) as a decoupled layer to handle cross-cutting concerns like authentication.
- Layered architecture: configurations at the base, followed by controllers → services → models → database.

##### Incorporate Relevant Technologies
- Node.js and Express for the server and routing.
- Babel for ES6+ code compilation.
- Sequelize as the ORM, supporting MySQL (or PostgreSQL).
- JWT authentication mechanism.
- Pug for web (template/view) rendering.
- Tools like nodemon for development and environmental configuration through .env and config files.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Bootstrap Layer
    subgraph "Bootstrap Layer"
        Configurations["Configurations (.env, configs directory)"]:::config
        App["Server Entry Point (app/app.js)"]:::app
    end

    %% Routing Layer
    subgraph "Routing Layer"
        APIRoutes["API Routes (app/routes/api)"]:::route
        WebRoutes["Web Routes (app/routes/web)"]:::route
        Policies["Policies (app/policies)"]:::policy
    end

    %% Controller Layer
    subgraph "Controller Layer"
        APIControllers["API Controllers (app/controllers/api)"]:::controller
        WebControllers["Web Controllers (app/controllers/web)"]:::controller
    end

    %% Business Logic Layer
    subgraph "Business Logic Layer"
        subgraph "Services"
            DBService["Database Service (db.service.js)"]:::service
            BcryptService["Password Hashing Service (bcrypt.service.js)"]:::service
            JWTService["JWT Service (jwt.service.js)"]:::service
        end
        Models["Models (Sequelize, app/models)"]:::model
        Facades["Facades & Factories (errors, responses, facades)"]:::factory
        Utilities["Utilities (app/utils)"]:::utility
    end

    %% Database Layer
    subgraph "Database Layer"
        Database["Database (MySQL/PostgreSQL)"]:::database
    end

    %% Presentation Layer
    subgraph "Presentation Layer"
        Views["Views (app/views)"]:::view
        Public["Public Assets (public)"]:::public
    end

    %% Operations Layer
    subgraph "Operations Layer"
        Migrations["Migrations/Seeders (migrator)"]:::ops
    end

    %% Connections
    Configurations --> App
    Configurations --> DBService
    Configurations --> Models
    App --> APIRoutes
    App --> WebRoutes
    App --> Public
    APIRoutes --> Policies
    APIRoutes --> APIControllers
    WebRoutes --> WebControllers
    Policies --> APIControllers
    APIControllers --> DBService
    APIControllers --> BcryptService
    APIControllers --> JWTService
    APIControllers --> Facades
    APIControllers --> Utilities
    DBService --> Models
    Models --> Database
    WebControllers --> Views
    Migrations --> Database
    DBService --> Utilities

    %% Click Events
    click App "https://github.com/markkhramko/nodejs-express-jwt/blob/master/app/app.js"
    click APIControllers "https://github.com/markkhramko/nodejs-express-jwt/tree/master/app/controllers/api"
    click WebControllers "https://github.com/markkhramko/nodejs-express-jwt/tree/master/app/controllers/web"
    click APIRoutes "https://github.com/markkhramko/nodejs-express-jwt/tree/master/app/routes/api"
    click WebRoutes "https://github.com/markkhramko/nodejs-express-jwt/tree/master/app/routes/web"
    click Models "https://github.com/markkhramko/nodejs-express-jwt/tree/master/app/models"
    click DBService "https://github.com/markkhramko/nodejs-express-jwt/blob/master/app/services/db.service.js"
    click BcryptService "https://github.com/markkhramko/nodejs-express-jwt/blob/master/app/services/bcrypt.service.js"
    click JWTService "https://github.com/markkhramko/nodejs-express-jwt/blob/master/app/services/jwt.service.js"
    click Facades "https://github.com/markkhramko/nodejs-express-jwt/tree/master/app/facades"
    click Policies "https://github.com/markkhramko/nodejs-express-jwt/tree/master/app/policies"
    click Configurations "https://github.com/markkhramko/nodejs-express-jwt/tree/master/configs"
    click Migrations "https://github.com/markkhramko/nodejs-express-jwt/tree/master/migrator"
    click Public "https://github.com/markkhramko/nodejs-express-jwt/tree/master/public"
    click Views "https://github.com/markkhramko/nodejs-express-jwt/tree/master/app/views"
    click Utilities "https://github.com/markkhramko/nodejs-express-jwt/tree/master/app/utils"

    %% Styles
    classDef config fill:#ffcc00,stroke:#b37f00,stroke-width:2px;
    classDef app fill:#ff9999,stroke:#ff3333,stroke-width:2px;
    classDef route fill:#cce5ff,stroke:#3399ff,stroke-width:2px;
    classDef controller fill:#99ccff,stroke:#0066cc,stroke-width:2px;
    classDef service fill:#ccffcc,stroke:#33cc33,stroke-width:2px;
    classDef model fill:#b3ffb3,stroke:#33cc33,stroke-width:2px;
    classDef factory fill:#e0ccff,stroke:#9933cc,stroke-width:2px;
    classDef utility fill:#ccffff,stroke:#339999,stroke-width:2px;
    classDef database fill:#d9d9d9,stroke:#828282,stroke-width:2px;
    classDef view fill:#ffd1dc,stroke:#ff6699,stroke-width:2px;
    classDef public fill:#ffffcc,stroke:#cccc66,stroke-width:2px;
    classDef ops fill:#ffcc99,stroke:#ff9933,stroke-width:2px;
    classDef policy fill:#ff9999,stroke:#ff3333,stroke-width:2px;
```
