# nestjsx/nest-router architecture

> Architecture diagram of the GitHub repository nestjsx/nest-router, 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/nestjsx/nest-router
- Repository: https://github.com/nestjsx/nest-router

## Overview

##### Main Components
- Library Core: Represent the “RouterModule” (found in src/index.ts, src/router.module.ts, etc.) as the primary component. This module exposes key functionality like “resolvePath()” and the API for defining routes.
- Route Definitions: Illustrate the route tree mechanism where routes are defined (for example, in a separate “routes.ts” file in each example). This shows that each NestJS module (i.e., NinjaModule, CatsModule, DogsModule) can have its own path, and these may be nested.
– Ninja Module (with its controllers, e.g., ninja.controller.ts and katana.controller.ts)
– Cats Module (including controllers like cats.controller.ts and ketty.controller.ts)
– Dogs Module (including controllers like dogs.controller.ts and puppy.controller.ts)
- Supporting and Configuration Files: Although not core to runtime, mention the presence of configuration files (tsconfig.json, tslint.json, jest.json) and build scripts (scripts/build.sh) that support development and deployment lifecycle.
- Testing: Indicate that there are unit and integration tests (e.g., in src/test). These ensure functionality for route resolution and path validation within the module.

##### Architectural Patterns
- Modular Design: Stress that the use of individual NestJS modules (each with its own controllers and maybe child modules) follows a modular and scalable architecture.
- Hierarchical Routing: Illustrate the parent-child relationship between modules. This hierarchical routing pattern is fundamental to the RouterModule’s functionality.
- Integration with NestJS: Note that despite providing enhanced routing, the module integrates seamlessly with the standard NestJS dependency injection and modular system.

##### Technology and Framework Details
- RouterModule: Emphasize that this is an add-on to NestJS (and starting with NestJS v8, part of @nestjs/core), and highlight functions such as “resolvePath” for middleware configuration.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entry
    WS("Web Server"):::system
    ENTRY("main.ts (Entry Point)"):::example

    %% Library Core Subgraph
    subgraph "Library Core (nest-router)"
        IDX("index.ts"):::core
        ROUTER("router.module.ts"):::core
        ROUTESINT("routes.interface.ts"):::core
        FLAT("flat-routes.util.ts"):::core
        VALIDATE("validate-path.util.ts"):::core
    end

    %% Route Definitions Subgraph
    subgraph "Route Definitions"
        RT4("nest-v4x routes.ts"):::routes
        RT5("nest-v5x routes.ts"):::routes
        RT5M("nest-v5x-m2m routes.ts"):::routes
    end

    %% Example Applications Subgraph
    subgraph "Example Applications"
        %% Entry point already declared above; linking inside for clarity
        ENTRY_EX("main.ts (Entry Point)"):::example
        %% Ninja Module
        subgraph "Ninja Module"
            NMOD("ninja.module.ts"):::example
            NCTRL("ninja.controller.ts"):::example
            KATANA("katana.controller.ts"):::example
        end
        %% Cats Module
        subgraph "Cats Module"
            CMOD("cats.module.ts"):::example
            CCTRL("cats.controller.ts"):::example
            KETTY("ketty.controller.ts"):::example
        end
        %% Dogs Module
        subgraph "Dogs Module"
            DMOD("dogs.module.ts"):::example
            DCTRL("dogs.controller.ts"):::example
            PUPPY("puppy.controller.ts"):::example
        end
    end

    %% Supporting and Configuration Files Subgraph
    subgraph "Supporting & Config Files"
        TSC("tsconfig.json / tsconfig.prod.json"):::config
        TSL("tslint.json"):::config
        PKGR("package.json (root)"):::config
        PKGE("package.json (example)"):::config
        BUILD("scripts/build.sh"):::config
    end

    %% Testing Subgraph
    subgraph "Testing"
        TSTR("router.module.spec.ts"):::testing
        TSTFL("flat-routes.spec.ts"):::testing
        TSTVAL("validate-path.spec.ts"):::testing
        E2E("app.e2e-spec.ts"):::testing
        JEST("jest-e2e.json"):::testing
    end

    %% Connections (Flow)
    WS -->|"HTTP_Request"| ENTRY
    ENTRY -->|"Imports_Configures"| ROUTER
    ROUTER -->|"ResolvesRoutes"| RT4
    ROUTER -->|"ResolvesRoutes"| RT5
    ROUTER -->|"ResolvesRoutes"| RT5M
    ENTRY -->|"Bootstraps"| NMOD
    NMOD -->|"Invokes"| NCTRL
    NMOD -->|"Invokes"| KATANA
    NMOD -->|"Nests"| CMOD
    NMOD -->|"Nests"| DMOD
    CMOD -->|"Invokes"| CCTRL
    CMOD -->|"Invokes"| KETTY
    DMOD -->|"Invokes"| DCTRL
    DMOD -->|"Invokes"| PUPPY

    %% Style Definitions
    classDef core fill:#FFF2CC,stroke:#D79B00,stroke-width:2px;
    classDef routes fill:#D0E9F2,stroke:#2E86AB,stroke-width:2px;
    classDef example fill:#D5F5E3,stroke:#1E8449,stroke-width:2px;
    classDef config fill:#E8F8F5,stroke:#117864,stroke-width:2px;
    classDef testing fill:#FADBD8,stroke:#C0392B,stroke-width:2px;
    classDef system fill:#FCF3CF,stroke:#B7950B,stroke-width:2px;

    %% Click Events for Library Core
    click IDX "https://github.com/nestjsx/nest-router/blob/master/src/index.ts"
    click ROUTER "https://github.com/nestjsx/nest-router/blob/master/src/router.module.ts"
    click ROUTESINT "https://github.com/nestjsx/nest-router/blob/master/src/routes.interface.ts"
    click FLAT "https://github.com/nestjsx/nest-router/blob/master/src/utils/flat-routes.util.ts"
    click VALIDATE "https://github.com/nestjsx/nest-router/blob/master/src/utils/validate-path.util.ts"

    %% Click Events for Route Definitions
    click RT4 "https://github.com/nestjsx/nest-router/blob/master/examples/nest-v4x/src/routes.ts"
    click RT5 "https://github.com/nestjsx/nest-router/blob/master/examples/nest-v5x/src/routes.ts"
    click RT5M "https://github.com/nestjsx/nest-router/blob/master/examples/nest-v5x-m2m/src/routes.ts"

    %% Click Events for Ninja Module
    click NMOD "https://github.com/nestjsx/nest-router/blob/master/examples/nest-v4x/src/ninja/ninja.module.ts"
    click NCTRL "https://github.com/nestjsx/nest-router/blob/master/examples/nest-v4x/src/ninja/ninja.controller.ts"
    click KATANA "https://github.com/nestjsx/nest-router/blob/master/examples/nest-v4x/src/ninja/katana.controller.ts"

    %% Click Events for Cats Module
    click CMOD "https://github.com/nestjsx/nest-router/blob/master/examples/nest-v4x/src/cats/cats.module.ts"
    click CCTRL "https://github.com/nestjsx/nest-router/blob/master/examples/nest-v4x/src/cats/cats.controller.ts"
    click KETTY "https://github.com/nestjsx/nest-router/blob/master/examples/nest-v4x/src/cats/ketty.controller.ts"

    %% Click Events for Dogs Module
    click DMOD "https://github.com/nestjsx/nest-router/blob/master/examples/nest-v4x/src/dogs/dogs.module.ts"
    click DCTRL "https://github.com/nestjsx/nest-router/blob/master/examples/nest-v4x/src/dogs/dogs.controller.ts"
    click PUPPY "https://github.com/nestjsx/nest-router/blob/master/examples/nest-v4x/src/dogs/puppy.controller.ts"

    %% Click Events for Supporting & Config Files
    click TSC "https://github.com/nestjsx/nest-router/blob/master/tsconfig.json"
    click TSL "https://github.com/nestjsx/nest-router/blob/master/tslint.json"
    click PKGR "https://github.com/nestjsx/nest-router/blob/master/package.json"
    click PKGE "https://github.com/nestjsx/nest-router/blob/master/examples/nest-v4x/package.json"
    click BUILD "https://github.com/nestjsx/nest-router/blob/master/scripts/build.sh"

    %% Click Events for Testing
    click TSTR "https://github.com/nestjsx/nest-router/blob/master/src/test/router.module.spec.ts"
    click TSTFL "https://github.com/nestjsx/nest-router/blob/master/src/test/utils/flat-routes.spec.ts"
    click TSTVAL "https://github.com/nestjsx/nest-router/blob/master/src/test/utils/validate-path.spec.ts"
    click E2E "https://github.com/nestjsx/nest-router/blob/master/examples/nest-v5x/test/app.e2e-spec.ts"
    click JEST "https://github.com/nestjsx/nest-router/blob/master/examples/nest-v5x/test/jest-e2e.json"
```
