Skip to content

minhvo99/natours-server

Repository files navigation

Natours

command build for windown

"build": "del /q dist\\* && npx tsc"

Nodejs version

node v18.20.4 (npm v10.7.0)

ENV EXAMPLE

MONGODB_PASSWORD=
MONGODB_HOST=
MONGODB_DB=
MONGODB_USER=
CLUSTER=
PORT=
NODE_ENV=

JWT_SECRET_KEY=
JWT_EXPIRE_IN=
JWT_REFRESH_KEY=

JWT_COOKIE_EXPIRE_IN=

EMAIL_USERNAME=
EMAIL_PASSWORD=
EMAIL_HOST=
EMAIL_PORT=

GOOGLE_CLIENT_ID=

API Documentation

Base URL

http://localhost:8080/api/v1

Authentication

Sign Up

POST /sign-up

Request body:

{
   "name": "John Doe",
   "email": "[email protected]",
   "password": "password123",
   "passWordConfirm": "password123"
}

Log In

POST /log-in

Request body:

{
   "email": "[email protected]",
   "password": "password123"
}

Google Login

POST /google-login

Request body:

{
   "idToken": "google-oauth-token"
}

Forgot Password

POST /forgot-password

Request body:

{
   "email": "[email protected]"
}

Reset Password

PATCH /reset-password/:token

Request body:

{
   "password": "newPassword123",
   "passWordConfirm": "newPassword123"
}

Change Password

PATCH /change-password Requires authentication

Request body:

{
   "passWordCurrent": "currentPassword",
   "password": "newPassword123",
   "passWordConfirm": "newPassword123"
}

Active Account

PATCH /active-account Requires authentication and admin role

Request body:

{
   "email": "[email protected]"
}

Tours

Get All Tours

GET /tour

Query parameters:

  • duration[lte]=5 - Filter by duration
  • difficulty=difficult - Filter by difficulty
  • name=The Snow Adventurer - Filter by name
  • sort=price,ratingsAverage - Sort results
  • fields=name,price - Select specific fields
  • page=2&limit=4 - Pagination

Get Tour by ID

GET /tour/:id

Get Top 5 Cheap Tours

GET /tour/top-5-cheap

Get Tour Statistics

GET /tour/tour-stast

Get Monthly Plan

GET /tour/monthly-plan/:year Requires authentication (admin, lead-guide, guide)

Get Tours Within Distance

GET /tour/tours-within/:distance/center/:latlng/unit/:unit

Example: /tour/tours-within/200/center/34.111745,-118.113491/unit/mi

Get Distances

GET /tour/distances/:latlng/unit/:unit

Example: /tour/distances/34.111745,-118.113491/unit/mi

Create Tour

POST /tour Requires authentication (admin, lead-guide)

Request body:

{
   "name": "The Sea Explorer",
   "duration": 7,
   "maxGroupSize": 15,
   "difficulty": "medium",
   "ratingsQuantity": 6,
   "ratingsAverage": 4.5,
   "price": 497,
   "summary": "Exploring the jaw-dropping US east coast by foot and by boat",
   "description": "Test",
   "imageCover": "tour-2-cover.jpg",
   "images": ["tour-2-1.jpg", "tour-2-2.jpg", "tour-2-3.jpg"],
   "startDates": [
      "2021-06-19T09:00:00.000Z",
      "2021-07-20T09:00:00.000Z",
      "2021-08-18T09:00:00.000Z"
   ]
}

Update Tour

PATCH /tour/:id Requires authentication (admin, lead-guide)

Supports multipart/form-data for image uploads:

  • imageCover - Single cover image
  • images - Multiple tour images (max 3)

Request body example:

{
   "name": "Updated Tour Name",
   "price": 599
}

Delete Tour

DELETE /tour/:id Requires authentication (admin, lead-guide)

Users

Get All Users

GET /user Requires authentication (admin)

Get Current User

GET /user/me Requires authentication

Get User by ID

GET /user/:id Requires authentication (admin)

Update My Profile

PATCH /user/update-profile Requires authentication

Supports multipart/form-data for photo upload:

  • photo - User profile photo

Request body:

{
   "name": "Updated Name",
   "email": "[email protected]"
}

Delete Me (Deactivate Account)

DELETE /user/delete-me Requires authentication

Reviews

Get All Reviews

GET /review Requires authentication

Get Reviews for Specific Tour

GET /tour/:id/reviews Requires authentication

Get Review by ID

GET /review/:id Requires authentication

Create Review

POST /review Requires authentication (guest)

POST /tour/:id/reviews Requires authentication (guest)

Request body:

{
   "review": "Amazing tour! Highly recommended.",
   "rating": 5
}

Update Review

PATCH /review/:id Requires authentication (guest - own reviews only)

Request body:

{
   "review": "Updated review text",
   "rating": 4
}

Delete Review

DELETE /review/:id Requires authentication (guest - own reviews only)

Bookings

Get All Bookings

GET /bookings Requires authentication (admin, lead-guide)

Get Booking by ID

GET /bookings/:id Requires authentication (admin, lead-guide)

Create Booking

POST /bookings Requires authentication (admin, lead-guide)

Request body:

{
   "tour": "tour-id",
   "user": "user-id",
   "price": 497
}

Update Booking

PATCH /bookings/:id Requires authentication (admin, lead-guide)

Delete Booking

DELETE /bookings/:id Requires authentication (admin, lead-guide)

Example Requests

### Get all tours with filters
GET http://localhost:8080/api/v1/tour?duration[lte]=5&difficulty=difficult

### Get tours by name
GET http://localhost:8080/api/v1/tour?name=The Snow Adventurer

### Get sorted tours
GET http://localhost:8080/api/v1/tour?sort=price,ratingsAverage

### Get specific fields only
GET http://localhost:8080/api/v1/tour?fields=name,price

### Pagination
GET http://localhost:8080/api/v1/tour?page=2&limit=4

### Get top 5 cheap tours
GET http://localhost:8080/api/v1/tour/top-5-cheap

### Get tour statistics
GET http://localhost:8080/api/v1/tour/tour-stast

### Get monthly plan
GET http://localhost:8080/api/v1/tour/monthly-plan/2021

### Delete tour
DELETE http://localhost:8080/api/v1/tour/66e01edbb827da77d259c699

Authentication Headers

For protected routes, include the JWT token in the Authorization header:

Authorization: Bearer <your-jwt-token>

User Roles

  • admin: Full access to all resources
  • lead-guide: Can manage tours and bookings
  • guide: Can view tours and monthly plans
  • guest: Can create reviews and view content

Error Responses

All errors follow this format:

{
   "status": "error",
   "message": "Error description"
}

Common HTTP status codes:

  • 400: Bad Request
  • 401: Unauthorized
  • 403: Forbidden
  • 404: Not Found
  • 500: Internal Server Error

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Contributors 2

  •  
  •