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.
- 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.jsonincluded)
| 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 |
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
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 installCreate 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) |
node productDB.js # loads products.json
node singleProductDB.js # loads singleProduct.json
⚠️ Each seed script runsdeleteMany()first, so it wipes the existing collection before inserting sample data.
npm run dev # development with nodemon
npm start # productionServer runs at http://localhost:5000.
| 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 |
| 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
}GET /api/singleProduct?id=productnoa
Returns an array with the matching product, including discount, quantity, rating, multiple image URLs and an HTML highlight block.
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
- Push the repo to GitHub.
- Import it in Vercel.
- Add
MONGODB_URLunder Settings → Environment Variables. - Deploy. In MongoDB Atlas, allow Vercel access (Network Access →
0.0.0.0/0).
- 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
Mayur Kapadi
- GitHub: @mkkapadi12
- Email: mayurkapadi12@gmail.com
⭐ If you find this useful, consider giving it a star!