# ab-434/url-shortener architecture

> Architecture diagram of the GitHub repository ab-434/url-shortener, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ab-434/url-shortener
- Repository: https://github.com/ab-434/url-shortener

## Overview

##### Project Type and Purpose
- This is a full-stack URL shortening service.
- Frontend: React single-page app for entering URLs, copying short links, and viewing click analytics.
- Backend: Node.js + Express REST API that generates and resolves short codes, stores data in MongoDB via Mongoose.

##### File Structure Analysis
- Top-level “backend/” directory: contains `index.js` (or `server.js`) entry point, model definitions under `models/shortUrl.js`, and Node package files.
- Top-level “frontend/” directory: standard Create-React-App layout with `public/` for static assets (`index.html`) and `src/` for React components (`App.js`, `index.js`, etc.).
- Configuration files: `.env` (for `MONGODB_URI`, `PORT`), `.gitignore`, `package.json` in each tier.

##### Key Architecture Insights from README
- API endpoints: POST `/shorten`, GET `/:shortCode`, GET `/api/urls`.
- Tech stack: React + Axios + Framer Motion on client; Express + Mongoose + ShortID on server; MongoDB database.

##### Main Components
a. Client Layer (React Single-Page App)
- UI components (URL input form, results list, analytics dashboard)
- HTTP client (Axios)
b. API Layer (Express Server)
- Route handlers for `/shorten`, `/:shortCode`, `/api/urls`
- Business logic for short code generation (ShortID) and click counting
c. Data Layer (MongoDB)
- `shortUrls` collection via Mongoose model `ShortUrl`
d. External/Infrastructure
- Environment (`.env`) for configuration
- Hosting or cloud services (e.g., MongoDB Atlas, Node host, static file server)

##### Relationships and Data Flow
- User enters a long URL in the React app → Axios POST to Express `/shorten`.
- Express service generates a short code, persists a document in MongoDB (via Mongoose), returns the shortened URL.
- React displays the new short URL; when a user clicks it, browser requests `GET /:shortCode` → Express looks up original URL in MongoDB and issues HTTP 302 redirect.
- Click counters are incremented in the same document, enabling analytics fetched via `GET /api/urls`.

##### Architectural Patterns and Principles
- Layered architecture: Presentation (React) → API (Express) → Persistence (MongoDB).
- Separation of concerns: UI logic isolated from server logic; data validation and business rules reside in the backend.
- Use of REST principles for HTTP API design.
- Asynchronous I/O throughout (Axios + Express handlers + Mongoose Promises).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Layer
    subgraph "Client Layer"
        direction TB
        ReactApp["React App"]:::client
        UI["UI Components\n(URL Form, Results, Analytics)"]:::client
        Axios["Axios HTTP Client"]:::client
        ReactApp --> UI
        ReactApp --> Axios
    end

    %% API Layer
    subgraph "API Layer"
        direction TB
        Express["Express Server\n(Node.js, Express.js)"]:::server
        Routes["Route Handlers\nPOST /shorten\nGET /:shortCode\nGET /api/urls"]:::server
        Business["ShortID Generator\n& Click Counting Logic"]:::server
        Model["Mongoose Model\n(ShortUrl)"]:::server
        Express --> Routes
        Routes --> Business
        Business --> Model
    end

    %% Database
    subgraph "Database"
        direction TB
        MongoDB["MongoDB"]:::database
        ShortUrls["shortUrls Collection"]:::database
        Model -->|"Reads/Writes via Mongoose"| ShortUrls
    end

    %% Config & Documentation
    subgraph "Config & Docs"
        direction TB
        Env["Environment Variables\n(.env)"]:::config
        GitRoot[".gitignore"]:::config
        GitFront["frontend/.gitignore"]:::config
        RootReadme["README.md"]:::config
        FrontReadme["frontend/README.md"]:::config
        FrontPkg["frontend/package.json"]:::config
        BackPkg["backend/package.json"]:::config
    end

    %% Data Flows
    Axios -->|POST /shorten| Routes
    Axios -->|GET /api/urls| Routes
    Axios -->|GET /:shortCode| Routes
    Routes -->|302 Redirect| UI

    %% Click Events
    click ReactApp "https://github.com/ab-434/url-shortener/tree/main/frontend/src/"
    click UI "https://github.com/ab-434/url-shortener/blob/main/frontend/src/App.js"
    click Axios "https://github.com/ab-434/url-shortener/blob/main/frontend/src/App.js"
    click Express "https://github.com/ab-434/url-shortener/blob/main/backend/index.js"
    click Model "https://github.com/ab-434/url-shortener/blob/main/backend/models/shortUrl.js"
    click FrontPkg "https://github.com/ab-434/url-shortener/blob/main/frontend/package.json"
    click BackPkg "https://github.com/ab-434/url-shortener/blob/main/backend/package.json"
    click GitRoot "https://github.com/ab-434/url-shortener/blob/main/.gitignore"
    click GitFront "https://github.com/ab-434/url-shortener/blob/main/frontend/.gitignore"
    click RootReadme "https://github.com/ab-434/url-shortener/blob/main/README.md"
    click FrontReadme "https://github.com/ab-434/url-shortener/blob/main/frontend/README.md"

    %% Styles
    classDef client fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef server fill:#90EE90,stroke:#333,stroke-width:1px
    classDef database fill:#FFA500,stroke:#333,stroke-width:1px
    classDef config fill:#D3D3D3,stroke:#333,stroke-width:1px
```
