# witzelfitz/nodeshortener architecture

> Architecture diagram of the GitHub repository witzelfitz/nodeshortener, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/witzelfitz/nodeshortener
- Repository: https://github.com/witzelfitz/nodeshortener

## Overview

Project Type: This is a full-stack web application that provides URL shortening functionality, similar to services like bit.ly but running locally.

##### Main Components

a. Client Layer:
- Frontend (Pug Templates)
- Static Assets (CSS)
- As the topmost layer

b. Application Layer:
- Express.js Server
- Route Handlers
- URL Validation (JOI)
- URL Processing Logic

c. Data Layer:
- MongoDB Database
- Mongoose Models

##### Component Relationships

- Client → Express Server (HTTP Requests)
- Express Server → Route Handlers
- Route Handlers → URL Model
- URL Model → MongoDB
- MongoDB → URL Model (Response)
- URL Model → Route Handlers
- Route Handlers → Client (Response)

##### Specific Implementation Details

- URL Shortening Logic (using nanoid)
- Validation Layer (JOI implementation)
- Redirect Handler (for short URLs)
- Template Engine (Pug)

##### Key Flows to Highlight

a. URL Shortening Flow:
1. Client submits long URL
2. Validation
3. Generation of short URL
4. Storage in MongoDB
5. Return short URL to client

b. URL Redirection Flow:
1. Client requests short URL
2. Lookup in database
3. Redirect to original URL

##### Technical Stack Indicators

Technology labels:
- Node.js/Express
- MongoDB/Mongoose
- Pug Template Engine
- JOI Validation
- nanoid (for URL generation)

##### Additional Elements

- Configuration settings
- Static file serving
- Error handling paths
- Middleware stack

- Presentation Layer (Pug templates, CSS)
- Application Layer (Express, Routes, Controllers)
- Data Access Layer (Mongoose Models)
- Database Layer (MongoDB)

Remember to:
- Use consistent symbols and notation
- Version information and date
- External dependencies or services

## Diagram (Mermaid)

```mermaid
%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#4f7cac', 'secondaryColor': '#98c1d9', 'tertiaryColor': '#e0fbfc'}}}%%

flowchart TB
    classDef client fill:#4f7cac,color:#fff
    classDef server fill:#98c1d9,color:#333
    classDef database fill:#e0fbfc,color:#333
    classDef validation fill:#ee6c4d,color:#fff
    classDef processing fill:#293241,color:#fff

    subgraph Client Layer
        FE[Frontend Templates]
        SA[Static Assets]
    end

    subgraph Application Layer
        ES[Express Server]
        RH[Route Handlers]
        
        subgraph Processing
            UV[URL Validation]
            USL[URL Shortening Logic]
            RDH[Redirect Handler]
        end
    end

    subgraph Data Layer
        UM[URL Model]
        DB[(MongoDB)]
    end

    %% Relationships
    FE --> ES
    SA --> ES
    ES --> RH
    RH --> UV
    UV --> USL
    USL --> UM
    RDH --> UM
    UM <--> DB
    UM --> RH
    RH --> FE

    %% Click Events
    click FE "https://github.com/Witzelfitz/nodeShortener/blob/main/06_final/views/index.pug"
    click SA "https://github.com/Witzelfitz/nodeShortener/blob/main/06_final/public/styles.css"
    click ES "https://github.com/Witzelfitz/nodeShortener/blob/main/06_final/index.js"
    click RH "https://github.com/Witzelfitz/nodeShortener/blob/main/06_final/routes/urls.js"
    click UM "https://github.com/Witzelfitz/nodeShortener/blob/main/06_final/models/url.js"

    %% Styling
    class FE,SA client
    class ES,RH server
    class UM,DB database
    class UV,USL validation
    class RDH processing

    %% Legend
    subgraph Legend
        C1[Client Components]:::client
        S1[Server Components]:::server
        D1[Data Components]:::database
        V1[Validation Components]:::validation
        P1[Processing Components]:::processing
    end
```
