# noor-2002/natours architecture

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

- Interactive diagram: https://gitdiagram.com/noor-2002/natours
- Repository: https://github.com/noor-2002/natours

## Overview

##### Main Components
- Client/Frontend:
- Static files (HTML, CSS, JS) under the "public" directory.
- Pug templates for server-rendered pages in the "views" and "dev-data/templates" directories.
- Server/Backend:
- Application entry points: "app.js" and "server.js".
- Express routes in the "routes" directory (bookingRoutes.js, reviewRoutes.js, tourRoutes.js, userRoutes.js, viewRoutes.js) that handle API endpoints and web page navigation.
- Controllers in the "controllers" directory implementing business logic (auth, tour, booking, review, error, views, etc.).
- Data models defined via Mongoose in the "models" directory, connecting with a MongoDB Atlas database.
- Utility functions and error handling utilities in the "utils" directory.
- Data Management:
- The "dev-data" directory includes sample dataset files for tours, reviews, and users, as well as scripts (like "import-dev-data.js") to populate the database.
- Third-Party Integrations:
- Authentication with JSON Web Tokens.
- Payment processing via Stripe.
- Email services (using Mailtrap or SendGrid as described in the build info).
- Configuration & Build Tools:
- ESLint, Prettier configurations and Parcel as a bundler (with commands specified in the README).
- Deployment via Heroku (deployment steps are in the README).

##### Understand Architectural Patterns & Data Flow
- MVC Pattern:
- The project clearly follows an MVC (Model-View-Controller) structure with directories for Models (database schema), Views (Pug templates and static pages), and Controllers (business logic).
- API endpoints vs. rendered views:
- Separate routes for handling API requests (e.g., /api/v1/tours) and view rendering for webpages.
- Dependency Flow:
- The controllers interact with the models for database operations.
- Routes are used to channel incoming requests to the appropriate controller methods.
- The frontend interacts with these endpoints (using AJAX, form submissions, etc.), and static assets are served from the "public" directory.

- Client-side Layer:
- Components like the login, tour overview, and booking pages.
- API Server:
- Node.js application (with "app.js" and "server.js") hosting Express.
- Database:
- External Services:
- Stripe for payment processing.
- Email services (Mailtrap/SendGrid) for notifications.
- Development/Build Tools:

##### Additional Details
- Use of the MVC pattern and separation of concerns.
- External integrations (Stripe, Email services).
- Marking the dev data seeding process (using "dev-data") as a one-off tool for setting up sample datasets.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Client Layer
    subgraph "Client Layer"
        CB["Client/Browser"]:::client
        StaticAssets["Static Assets"]:::frontend
        PugViews["Pug Templates (views)"]:::frontend
        PugDev["Pug Templates (dev-data)"]:::frontend
    end

    %% Server Layer (Natours Backend)
    subgraph "Server Layer (Natours Backend)"
        subgraph "Entry Points"
            EP_app["app.js"]:::server
            EP_server["server.js"]:::server
        end
        subgraph "Routes"
            R_booking["bookingRoutes.js"]:::routes
            R_review["reviewRoutes.js"]:::routes
            R_tour["tourRoutes.js"]:::routes
            R_user["userRoutes.js"]:::routes
            R_view["viewRoutes.js"]:::routes
        end
        subgraph "Controllers"
            C_auth["authController.js"]:::controllers
            C_booking["bookingController.js"]:::controllers
            C_error["errorController.js"]:::controllers
            C_handler["handlerFactory.js"]:::controllers
            C_review["reviewController.js"]:::controllers
            C_tour["tourController.js"]:::controllers
            C_user["userController.js"]:::controllers
            C_views["viewsController.js"]:::controllers
        end
        subgraph "Models"
            M_booking["bookingModel.js"]:::models
            M_review["reviewModel.js"]:::models
            M_tour["tourModel.js"]:::models
            M_user["userModel.js"]:::models
        end
        subgraph "Utilities"
            U_api["apiFeatures.js"]:::utils
            U_error["appError.js"]:::utils
            U_async["catchAsync.js"]:::utils
            U_email["email.js"]:::utils
        end
    end

    %% Data Management
    subgraph "Data Management"
        DM["Dev Data & Seeding"]:::build
    end

    %% External Services
    subgraph "External Services"
        Stripe["Stripe Payment"]:::external
        Email["Email Service"]:::external
    end

    %% Build & Deployment
    subgraph "Build & Deployment"
        Build["Parcel, ESLint, Prettier"]:::build
        Deploy["Heroku Deployment"]:::build
    end

    %% Database
    subgraph "Database"
        DB["MongoDB Atlas"]:::db
    end

    %% Relationships
    CB -->|"HTTPRequests"| EP_app
    CB -->|"HTTPRequests"| EP_server
    StaticAssets --> CB
    PugViews --> CB
    PugDev --> CB

    EP_app --> R_booking
    EP_app --> R_review
    EP_app --> R_tour
    EP_app --> R_user
    EP_app --> R_view
    EP_server --> R_booking
    EP_server --> R_review
    EP_server --> R_tour
    EP_server --> R_user
    EP_server --> R_view

    R_booking --> C_booking
    R_review --> C_review
    R_tour --> C_tour
    R_user --> C_user
    R_view --> C_views

    C_auth --> M_user
    C_booking --> M_booking
    C_review --> M_review
    C_tour --> M_tour
    C_user --> M_user
    C_views --> M_tour

    M_booking --> DB
    M_review --> DB
    M_tour --> DB
    M_user --> DB

    C_booking --> Stripe
    C_auth --> Email

    C_auth --> U_async
    C_booking --> U_async
    C_review --> U_async
    C_tour --> U_async
    C_user --> U_async
    C_auth --> U_email

    DM --> DB

    Build --> EP_app
    Deploy --> EP_server

    C_booking -->|"Response"| CB
    C_review -->|"Response"| CB
    C_tour -->|"Response"| CB
    C_user -->|"Response"| CB
    C_views -->|"RenderedPage"| CB

    %% Click Events
    click StaticAssets "https://github.com/noor-2002/natours/tree/master/public/"
    click PugViews "https://github.com/noor-2002/natours/tree/master/views/"
    click PugDev "https://github.com/noor-2002/natours/tree/master/dev-data/templates/"
    click EP_app "https://github.com/noor-2002/natours/blob/master/app.js"
    click EP_server "https://github.com/noor-2002/natours/blob/master/server.js"
    click R_booking "https://github.com/noor-2002/natours/blob/master/routes/bookingRoutes.js"
    click R_review "https://github.com/noor-2002/natours/blob/master/routes/reviewRoutes.js"
    click R_tour "https://github.com/noor-2002/natours/blob/master/routes/tourRoutes.js"
    click R_user "https://github.com/noor-2002/natours/blob/master/routes/userRoutes.js"
    click R_view "https://github.com/noor-2002/natours/blob/master/routes/viewRoutes.js"
    click C_auth "https://github.com/noor-2002/natours/blob/master/controllers/authController.js"
    click C_booking "https://github.com/noor-2002/natours/blob/master/controllers/bookingController.js"
    click C_error "https://github.com/noor-2002/natours/blob/master/controllers/errorController.js"
    click C_handler "https://github.com/noor-2002/natours/blob/master/controllers/handlerFactory.js"
    click C_review "https://github.com/noor-2002/natours/blob/master/controllers/reviewController.js"
    click C_tour "https://github.com/noor-2002/natours/blob/master/controllers/tourController.js"
    click C_user "https://github.com/noor-2002/natours/blob/master/controllers/userController.js"
    click C_views "https://github.com/noor-2002/natours/blob/master/controllers/viewsController.js"
    click M_booking "https://github.com/noor-2002/natours/blob/master/models/bookingModel.js"
    click M_review "https://github.com/noor-2002/natours/blob/master/models/reviewModel.js"
    click M_tour "https://github.com/noor-2002/natours/blob/master/models/tourModel.js"
    click M_user "https://github.com/noor-2002/natours/blob/master/models/userModel.js"
    click U_api "https://github.com/noor-2002/natours/blob/master/utils/apiFeatures.js"
    click U_error "https://github.com/noor-2002/natours/blob/master/utils/appError.js"
    click U_async "https://github.com/noor-2002/natours/blob/master/utils/catchAsync.js"
    click U_email "https://github.com/noor-2002/natours/blob/master/utils/email.js"
    click DM "https://github.com/noor-2002/natours/tree/master/dev-data/"

    %% Styles
    classDef client fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef frontend fill:#85C1E9,stroke:#2874A6,stroke-width:2px;
    classDef server fill:#ABEBC6,stroke:#239B56,stroke-width:2px;
    classDef routes fill:#F7DC6F,stroke:#B7950B,stroke-width:2px;
    classDef controllers fill:#F5B7B1,stroke:#C0392B,stroke-width:2px;
    classDef models fill:#D7BDE2,stroke:#8E44AD,stroke-width:2px;
    classDef utils fill:#FAD7A0,stroke:#D68910,stroke-width:2px;
    classDef db fill:#D5DBDB,stroke:#7F8C8D,stroke-width:2px;
    classDef external fill:#F1948A,stroke:#922B21,stroke-width:2px;
    classDef build fill:#D2B4DE,stroke:#6C3483,stroke-width:2px;
```
