# shadlia/natours-api architecture

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

- Interactive diagram: https://gitdiagram.com/shadlia/natours-api
- Repository: https://github.com/shadlia/natours-api

## Overview

##### Project Type and Purpose
- This is a Node.js/Express backend for a tour‐booking application. It exposes RESTful APIs (tours, users, reviews, authentication) and also serves static assets (HTML/CSS/JS) and server‐side templates (Pug) for emails and page rendering. It follows the MVC pattern and integrates with MongoDB and an external email service.

##### High-Level Architectural Components
a. Client Layer
- Browser/mobile client rendering static HTML/CSS/JS from the “public” folder.
- Interacts with backend via HTTP/HTTPS API calls (JSON).
b. Web/API Server (Express)
- Entry points: server.js → app.js
- Middlewares: security, logging, JSON/body parsing, static file serving.
- Routing layer: Routes/*.js map URL paths to controllers.
- Controllers: controllers/*Controller.js implement business logic (auth, tours, users, reviews).
- Models: Models/*.js define Mongoose schemas and database operations.
- Utilities: utils/*.js (error handling, async wrapper, email sender, API feature filters).
- Templating: dev-data/templates/*.pug for generating dynamic HTML emails and pages.
c. Data Layer
- MongoDB database (hosted or local) storing collections: tours, users, reviews.
- dev-data import script (dev-data/data/import-dev-data.js) to seed initial data.
d. External Services
- SMTP/email provider (via utils/email.js) to send confirmation, password resets.
- Potential payment gateway (not shown but likely integrated alongside tours booking).

##### Relationships and Data Flow
1. Client → Express Server (HTTP request for page or API)
2. Express Middleware (auth, security, body parsing)
3. Routing Layer dispatches to the correct Controller
4. Controller uses Models to read/write MongoDB
5. Controller may invoke Utils (e.g., email sender, API filtering)
6. Model communicates with MongoDB (queries/inserts/updates)
7. Controller responds → JSON or rendered Pug template → back to Client
8. For emails: Controller triggers utils/email → SMTP service → end user

##### Architectural Patterns and Design Principles
- MVC separation: Routes → Controllers → Models
- Layered architecture: clear separation of concerns (routing, business logic, data access, utilities).
- Modularization: one folder per concern (e.g., authController, tourController).
- Configuration via environment (config.env) to manage secrets and env-specific settings.
- Error handling: centralized errorController and AppError utility.
- Data import utility for development workflows.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Layer"
        Client["Browser/Client UI"]:::client
        Static["Static Assets (public)"]:::client
    end
    Client -->|"GET /public"| Static
    Client -->|"HTTP/HTTPS API calls"| RoutingLayer

    subgraph "Express Server"
        ConfigEnv["config.env"]:::config
        ServerJS["server.js"]:::server
        AppJS["app.js"]:::server

        subgraph "Routing Layer"
            RoutingLayer(("Routing Module")):::routing
            TourRoute["tourRoute.js"]:::routing
            UserRoute["userRoute.js"]:::routing
            ReviewRoute["reviewRoute.js"]:::routing
        end

        subgraph "Controllers"
            TourC["tourController.js"]:::controller
            UserC["userController.js"]:::controller
            ReviewC["reviewController.js"]:::controller
            AuthC["authController.js"]:::controller
            ErrorC["errorController.js"]:::controller
        end

        subgraph "Models"
            TourM["tourModel.js"]:::model
            UserM["userModel.js"]:::model
            ReviewM["reviewModel.js"]:::model
        end

        subgraph "Utilities"
            APIF["apiFeatures.js"]:::util
            AppErr["appError.js"]:::util
            CatchA["catchAsync.js"]:::util
            EmailU["email.js"]:::util
            UserF["userFormatter.js"]:::util
        end

        subgraph "Templating"
            Templates(("Pug Templates")):::template
        end
    end

    RoutingLayer -->|routes to| TourC
    RoutingLayer -->|routes to| UserC
    RoutingLayer -->|routes to| ReviewC
    RoutingLayer -->|routes to| AuthC
    RoutingLayer -->|routes to| ErrorC

    TourC -->|CRUD operations| TourM
    UserC -->|CRUD operations| UserM
    ReviewC -->|CRUD operations| ReviewM

    AuthC -->|uses utils| EmailU
    AuthC -->|uses utils| APIF
    AuthC -->|uses utils| CatchA
    AuthC -->|uses utils| AppErr
    ErrorC -->|uses utils| AppErr

    TourC -->|"renders Pug"| Templates
    TourC -->|"send email"| EmailU

    subgraph "Data Layer"
        DB[(MongoDB Database)]:::db
        Import["import-dev-data.js"]:::data
        Seeds["Seed Data Files"]:::data
    end

    Import -->|seeds data| DB
    Seeds -->|used by| Import

    TourM -->|queries| DB
    UserM -->|queries| DB
    ReviewM -->|queries| DB

    subgraph "External Services"
        SMTP["SMTP/Email Service"]:::external
    end

    EmailU -->|"SMTP"| SMTP

    click Static "https://github.com/shadlia/natours-api/tree/main/public/"
    click ServerJS "https://github.com/shadlia/natours-api/blob/main/server.js"
    click AppJS "https://github.com/shadlia/natours-api/blob/main/app.js"
    click ConfigEnv "https://github.com/shadlia/natours-api/blob/main/config.env"
    click TourRoute "https://github.com/shadlia/natours-api/blob/main/Routes/tourRoute.js"
    click UserRoute "https://github.com/shadlia/natours-api/blob/main/Routes/userRoute.js"
    click ReviewRoute "https://github.com/shadlia/natours-api/blob/main/Routes/reviewRoute.js"
    click TourC "https://github.com/shadlia/natours-api/blob/main/controllers/tourController.js"
    click UserC "https://github.com/shadlia/natours-api/blob/main/controllers/userController.js"
    click ReviewC "https://github.com/shadlia/natours-api/blob/main/controllers/reviewController.js"
    click AuthC "https://github.com/shadlia/natours-api/blob/main/controllers/authController.js"
    click ErrorC "https://github.com/shadlia/natours-api/blob/main/controllers/errorController.js"
    click TourM "https://github.com/shadlia/natours-api/blob/main/Models/tourModel.js"
    click UserM "https://github.com/shadlia/natours-api/blob/main/Models/userModel.js"
    click ReviewM "https://github.com/shadlia/natours-api/blob/main/Models/reviewModel.js"
    click APIF "https://github.com/shadlia/natours-api/blob/main/utils/apiFeatures.js"
    click AppErr "https://github.com/shadlia/natours-api/blob/main/utils/appError.js"
    click CatchA "https://github.com/shadlia/natours-api/blob/main/utils/catchAsync.js"
    click EmailU "https://github.com/shadlia/natours-api/blob/main/utils/email.js"
    click UserF "https://github.com/shadlia/natours-api/blob/main/utils/userFormatter.js"
    click Templates "https://github.com/shadlia/natours-api/tree/main/dev-data/templates/"
    click Import "https://github.com/shadlia/natours-api/blob/main/dev-data/data/import-dev-data.js"
    click Seeds "https://github.com/shadlia/natours-api/tree/main/dev-data/data/"
    
    classDef client fill:#f2f2f2,stroke:#000
    classDef server fill:#cfe2f3,stroke:#000
    classDef config fill:#d9d2e9,stroke:#000
    classDef routing fill:#9fc5e8,stroke:#000
    classDef controller fill:#6fa8dc,stroke:#000
    classDef model fill:#3d85c6,stroke:#000
    classDef util fill:#93c47d,stroke:#000
    classDef template fill:#f9cb9c,stroke:#000
    classDef db fill:#b6d7a8,stroke:#000
    classDef external fill:#f4cccc,stroke:#000
    classDef data fill:#e6b8af,stroke:#000
```
