How shadlia/natours-api works
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.