ProjectsJanuary 21, 2025

Natours API - RESTful Tour Booking Backend

Natours API - RESTful Tour Booking Backend

Overview

Natours API is a production-ready RESTful API backend for a tour booking platform. Built following industry best practices, it demonstrates expertise in backend development, database design, authentication, and API architecture. The API supports comprehensive tour management, user authentication, booking operations, and payment processing.

Key Features

  • RESTful API Design: Clean, well-structured REST endpoints following best practices
  • User Authentication: Secure JWT-based authentication with password encryption
  • Tour Management: CRUD operations for tours with rich metadata
  • Booking System: Complete booking workflow with availability management
  • Payment Integration: Secure payment processing integration
  • Advanced Filtering: Complex query filtering, sorting, and pagination
  • Geospatial Features: Location-based queries using MongoDB geospatial indexes
  • Data Validation: Comprehensive input validation and sanitization
  • Error Handling: Robust error handling with meaningful error messages
  • Security: Protection against common vulnerabilities (XSS, NoSQL injection, etc.)
  • Performance: Optimized queries with indexing and aggregation pipelines

Technologies Used

  • Runtime: Node.js
  • Framework: Express.js
  • Database: MongoDB with Mongoose ODM
  • Authentication: JWT (JSON Web Tokens)
  • Password Hashing: bcrypt
  • Validation: Joi or express-validator
  • Security: helmet, express-rate-limit, express-mongo-sanitize
  • Geospatial: MongoDB geospatial queries
  • File Upload: multer for image uploads
  • Email: Nodemailer for email notifications

Technical Highlights

  • MVC Architecture: Clean separation of concerns with Models, Views (API responses), and Controllers
  • Middleware Stack: Custom middleware for authentication, error handling, and request processing
  • Database Design: Optimized schema design with proper indexing
  • API Versioning: Support for API versioning for backward compatibility
  • Documentation: Comprehensive API documentation
  • Error Handling: Centralized error handling with custom error classes
  • Security Best Practices: Protection against common web vulnerabilities
  • Performance Optimization: Query optimization, caching strategies, and efficient data loading

API Endpoints

Authentication

  • POST /api/v1/users/signup - User registration
  • POST /api/v1/users/login - User login
  • POST /api/v1/users/forgotPassword - Password reset request
  • PATCH /api/v1/users/resetPassword/:token - Password reset

Tours

  • GET /api/v1/tours - Get all tours with filtering, sorting, pagination
  • GET /api/v1/tours/:id - Get single tour
  • POST /api/v1/tours - Create tour (admin only)
  • PATCH /api/v1/tours/:id - Update tour (admin only)
  • DELETE /api/v1/tours/:id - Delete tour (admin only)

Bookings

  • GET /api/v1/bookings - Get all bookings
  • POST /api/v1/bookings - Create booking
  • GET /api/v1/bookings/:id - Get single booking
  • PATCH /api/v1/bookings/:id - Update booking
  • DELETE /api/v1/bookings/:id - Cancel booking

Reviews

  • GET /api/v1/reviews - Get all reviews
  • POST /api/v1/reviews - Create review
  • GET /api/v1/reviews/:id - Get single review
  • PATCH /api/v1/reviews/:id - Update review
  • DELETE /api/v1/reviews/:id - Delete review

Architecture

The API follows a scalable architecture:
  • Layered Architecture: Separation of routes, controllers, services, and models
  • Middleware Chain: Request processing through authentication, validation, and error handling
  • Database Layer: Mongoose models with schema validation
  • Business Logic: Service layer for complex business operations
  • Error Handling: Global error handler with proper HTTP status codes
  • Security Layer: Multiple security middleware for protection

Advanced Features

Geospatial Queries

  • Find tours within a certain distance from a location
  • Calculate distances between locations
  • Location-based tour recommendations

Aggregation Pipeline

  • Complex data aggregation for statistics
  • Tour ratings calculation
  • Revenue and booking analytics
  • Monthly tour statistics

Filtering and Sorting

  • Filter by price range, difficulty, duration
  • Sort by price, ratings, popularity
  • Pagination with limit and skip
  • Field limiting for optimized responses

Challenges and Solutions

Challenge 1: Complex Query Building

Problem: Building flexible queries with multiple filters, sorting, and pagination
Solution: Implemented query builder pattern that constructs MongoDB queries dynamically based on request parameters. Added support for advanced filtering, sorting, and field selection.

Challenge 2: Geospatial Data

Problem: Efficiently querying tours based on geographic location
Solution: Used MongoDB geospatial indexes and queries to find tours within specified distances. Implemented efficient distance calculations using geospatial operators.

Challenge 3: Security

Problem: Protecting against common web vulnerabilities
Solution: Implemented multiple security layers including helmet for HTTP headers, express-rate-limit for DDoS protection, express-mongo-sanitize for NoSQL injection prevention, and parameter validation.

Challenge 4: Performance

Problem: Handling large datasets efficiently
Solution: Implemented database indexing on frequently queried fields, used aggregation pipelines for complex queries, and added pagination to limit response sizes.

Real-World Applications

This API demonstrates skills applicable to:
  • E-commerce Backends: Similar structure for product management
  • Booking Platforms: Hotels, flights, events booking systems
  • Location-Based Services: Apps requiring geospatial queries
  • SaaS Platforms: Multi-tenant applications with user management
  • Enterprise APIs: Large-scale API development

Outcome

Natours API successfully demonstrates:
  • Proficiency in Node.js and Express.js backend development
  • Understanding of RESTful API design principles
  • Database design and optimization skills
  • Security implementation and best practices
  • Authentication and authorization patterns
  • Performance optimization techniques
  • API documentation and testing
The project showcases production-ready backend development skills, making it an excellent portfolio piece for backend or full-stack developer roles.

Repository

View on GitHub

Related projects

SyncApp - Multi-Platform Blog Syndication Platform

A full-stack blog posts syndication platform that allows writers to publish content to multiple platforms simultaneously. Built with React, Node.js, Express, and MongoDB, featuring rich text editing, Markdown support, and integrations with Medium, DEV.to, and WordPress.

CityPulseApp - React Native Event Discovery App

A modern React Native mobile application for discovering and managing local events. Built with Expo, featuring event discovery via Ticketmaster API, user authentication, favorites management, biometric authentication, multi-language support, and offline capabilities.

Music App - Modern Music Player Application

A modern music player application built with React, featuring music playback, playlist management, library organization, and a beautiful user interface. Demonstrates audio handling, state management, and responsive design.

GitHub Finder - GitHub Dashboard with Context API

A GitHub dashboard application that uses the GitHub API to display user profiles, repositories, and statistics. Built with React, Context API for state management, and modern UI components.

ContactKeeper - MERN Stack Contact Management App

A full-stack contact management application built with MERN stack (MongoDB, Express.js, React, Node.js) featuring Material UI for a modern, responsive interface.

DJ Event Frontend - Next.js Event Management

A modern event management frontend application built with Next.js and TypeScript. Features server-side rendering, static site generation, and optimized performance for event listings and management.

Budgety - Budget Management App in React

A personal budget management application built with React, helping users track income, expenses, and calculate budgets. Features an intuitive interface for financial planning and tracking.

IT Logger - Redux & Redux Thunk Application

An IT logging application built with React and Redux for state management, featuring Redux Thunk for handling asynchronous actions. Demonstrates advanced state management patterns.

Todo App - React + Vite + TailwindCSS

A modern, fast Todo application built with React, Vite for lightning-fast development, and TailwindCSS for beautiful, utility-first styling. Demonstrates modern React patterns and build tooling.