# poorv25/natours architecture

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

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

## Overview

##### Project Type and Purpose
- The README tells us that the project is built using modern technologies (Node.js, Express, Mongoose, MongoDB) which makes it a full-stack web application.
- The project clearly follows an MVC-like structure with controllers, models, views, as well as routes to handle different endpoints.
- It also seems to include functionalities like authentication, tour listings, reviews, bookings, and user account management.

##### Main Components
a. Client/Frontend:
- Views: The views directory (with Pug templates like account.pug, overview.pug, tour.pug) and the public directory (static assets such as CSS, JS files, images).
- Templating Engine: Pug templates rendering the UI and email templates.

b. Server/Backend:
- Application Entry Points: app.js and server.js serve as the main starting files.
- Controllers: Organize business logic (authController, bookingController, reviewController, tourController, userController, and viewsController for rendering).
- Routes: Files in the routes folder show how HTTP routes are organized (bookingRoutes.js, reviewRoutes.js, tourRouter.js, userRouter.js, viewRoutes.js).
- Models: Mongoose models (tourModel, userModel, bookingModel, reviewModel) that manage data interaction with MongoDB.
- Utilities: The utils folder with modules for error handling (appError.js, catchAsync.js), API features filtering (apifeatures.js), email sending (email.js) reveals design patterns for error and exception management.

c. Data Layer:
- Database: Although not a file in the tree, reading the README tells you it uses MongoDB with Mongoose. It is responsible for storing tour, user, booking, and review data.

d. External Services/Integrations:
- Email Service: The email module (under utils/email.js) along with dedicated email templates.
- Payment Integration: The Stripe related file (public/js/stripe.js) suggests payment processing integration.
- Integration: The presence of mapbox.js (public/js/mapbox.js) indicates some form of geographic mapping.
- Development Data: The dev-data folder contains sample/import scripts, JSON data, and images used to seed the application for development/testing.

##### Relationships and Interactions
1. Client request from a web browser (served via static assets from public & rendered via Pug views)
2. The request is routed through the Express routes which then call their corresponding controllers.
3. Controllers invoke Mongoose models to interact with MongoDB.
4. Responses are then rendered back via Pug templates or JSON responses if it’s an API.
- External integrations (such as email, payments, and maps) should be clearly shown as distinct services invoked by specific controllers or utility functions.

##### Notable Architectural Patterns and Principles
- MVC Pattern: Separate folders for controllers (handling business logic), models (data management), and views (presentation).
- Separation of Concerns: Clear isolation between route definitions, logic controllers, and model/database access.
- Error Handling and Async Patterns: Use of utilities like catchAsync and appError illustrate a robust error handling strategy.
- Modular Design: The organized file hierarchy (with clear separation between dev-data, public assets, and the utility modules) suggests a focus on maintainability.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Client/Frontend
    subgraph "Client/Frontend"
        Browser["Browser/Client"]:::client
        Static["Static Assets"]:::client
        Views["Views (Pug Templates)"]:::client
    end

    %% Server/Backend
    subgraph "Server/Backend"
        EP["Entry Points (app.js, server.js)"]:::backend
        Routes["Routes"]:::backend
        Controllers["Controllers"]:::backend
        Models["Models"]:::backend
        Utilities["Utilities"]:::backend
    end

    %% External & Data Layer
    subgraph "External & Data Layer"
        Database["MongoDB"]:::external
        Email["Email Service"]:::external
        Stripe["Stripe Payment"]:::external
        Map["Map Integration"]:::external
        DevData["Dev Data & Templates"]:::external
    end

    %% Data Flow
    Browser -->|"HTTP Request"| EP
    Browser -->|"Load Assets"| Static
    EP -->|"Route Handling"| Routes
    Routes -->|"Delegate"| Controllers
    Controllers -->|"Invoke Model Operations"| Models
    Models -->|"CRUD Operations"| Database
    Database -->|"Return Data"| Models
    Controllers -->|"Render Template"| Views
    Views -->|"Send Rendered HTML"| Browser
    Controllers -->|"Error/Email Handling"| Utilities
    Utilities -->|"Trigger Email"| Email
    Static -->|"Load Payment Script"| Stripe
    Static -->|"Load Map Script"| Map
    DevData -->|"Seed Database"| Database

    %% Click Events
    click Views "https://github.com/poorv25/natours/tree/master/views/"
    click Static "https://github.com/poorv25/natours/tree/master/public/"
    click EP "https://github.com/poorv25/natours/blob/master/app.js"
    click Controllers "https://github.com/poorv25/natours/tree/master/controllers/"
    click Routes "https://github.com/poorv25/natours/tree/master/routes/"
    click Models "https://github.com/poorv25/natours/tree/master/models/"
    click Utilities "https://github.com/poorv25/natours/tree/master/utils/"
    click DevData "https://github.com/poorv25/natours/tree/master/dev-data/"
    click Stripe "https://github.com/poorv25/natours/blob/master/public/js/stripe.js"
    click Map "https://github.com/poorv25/natours/blob/master/public/js/mapbox.js"

    %% Styles
    classDef client fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef backend fill:#d4edda,stroke:#333,stroke-width:2px;
    classDef external fill:#f8d7da,stroke:#333,stroke-width:2px;
```
