Skip to content
mkkapadi12Public

About

A RESTful backend built with Node.js, Express and MongoDB (Mongoose) that serves product data for an e-commerce storefront (latkan / tassel products). It supports searching, sorting, field selection and pagination, and can be deployed on Vercel.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

🛍️ Product REST API

A RESTful backend built with Node.js, Express and MongoDB (Mongoose) that serves product data for an e-commerce storefront (latkan / tassel products). It supports searching, sorting, field selection and pagination, and can be deployed on Vercel.

✨ Features

  • List products with search by name (case-insensitive regex)
  • Sort, select fields and paginate through query parameters
  • Separate endpoint for single product details (images, discount, quantity, highlights)
  • MongoDB with Mongoose schemas and validation
  • Seed scripts to load sample data from JSON files
  • CORS enabled for frontend apps
  • Vercel-ready (vercel.json included)

🛠️ Tech Stack

Technology Usage
Node.js Runtime
Express 4 Server and routing
MongoDB + Mongoose 8 Database and schemas
dotenv Environment variables
CORS Cross-origin requests
Nodemon Auto-restart in development

📁 Project Structure

REST-API/
├── DB/
│   └── connect.js            # MongoDB connection
├── controller/
│   ├── products.js           # Product list logic (search, sort, select, pagination)
│   └── singleProduct.js      # Single product logic
├── models/
│   ├── product.js            # Product schema
│   └── singleProduct.js      # Detailed product schema
├── routes/
│   ├── products.js
│   └── singleProducts.js
├── products.json             # Sample data (19 products)
├── singleProduct.json        # Sample data (8 detailed products)
├── productDB.js              # Seed script for products
├── singleProductDB.js        # Seed script for single products
├── index.js                  # App entry point
└── vercel.json               # Vercel deployment config

🚀 Getting Started

Prerequisites: Node.js 18+ and a MongoDB database (MongoDB Atlas or local).

# 1. Clone the repository
git clone https://github.com/mkkapadi12/REST-API.git

# 2. Go to the project folder
cd REST-API

# 3. Install dependencies
npm install

Environment variables

Create a .env file in the project root:

MONGODB_URL=your_mongodb_connection_string
PORT=5000
Variable Required Description
MONGODB_URL Yes MongoDB connection string
PORT No Server port (default 5000)

Seed the database (optional)

node productDB.js         # loads products.json
node singleProductDB.js   # loads singleProduct.json

⚠️ Each seed script runs deleteMany() first, so it wipes the existing collection before inserting sample data.

Run the server

npm run dev     # development with nodemon
npm start       # production

Server runs at http://localhost:5000.

📡 API Endpoints

Method Endpoint Description
GET / Health check (Hello World!)
GET /api/products Get products (search, sort, select, pagination)
GET /api/products/testing Get name and price of products, query passed directly to MongoDB
GET /api/singleProduct Get detailed product data, filter by id

Query parameters for /api/products

Param Example Description
name ?name=spring Case-insensitive search in product name
sort ?sort=price or ?sort=-price Sort ascending or descending
select ?select=name,price,image Return only chosen fields
page ?page=2 Page number (default 1)
limit ?limit=10 Items per page (default 20)

Combine freely:

GET /api/products?name=topa&sort=-price&select=name,price&page=1&limit=5

Response

{
  "Products": [
    { "name": "Mota Topa", "price": 360 }
  ],
  "nbHits": 1
}

Single product

GET /api/singleProduct?id=productnoa

Returns an array with the matching product, including discount, quantity, rating, multiple image URLs and an HTML highlight block.

🗃️ Data Models

Product: id, name, price, MRP, colors[], image, description, category (default latkan), shipping, featured, rating (default 4.9), createdAt

SingleProduct: id, name, price, MRP, discount, colors[], description, category, featured, quantity, rating, highlight, image[], createdAt

☁️ Deployment (Vercel)

  1. Push the repo to GitHub.
  2. Import it in Vercel.
  3. Add MONGODB_URL under Settings → Environment Variables.
  4. Deploy. In MongoDB Atlas, allow Vercel access (Network Access → 0.0.0.0/0).

🔮 Future Improvements

  • Full CRUD (POST, PUT, DELETE) with authentication
  • Filter by category, price range and featured
  • Input validation and centralised error handling
  • Rate limiting and security headers (helmet)
  • API documentation with Swagger

👤 Author

Mayur Kapadi


⭐ If you find this useful, consider giving it a star!

About

A RESTful backend built with Node.js, Express and MongoDB (Mongoose) that serves product data for an e-commerce storefront (latkan / tassel products). It supports searching, sorting, field selection and pagination, and can be deployed on Vercel.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages