A comprehensive, production-ready RESTful API built with Node.js and Express — designed to showcase advanced backend features and clean architecture.
- ✅ Advanced Filtering: Filter products by name, company, or featured status.
- ✅ Complex Numeric Filtering: Apply numeric filters on price and rating using operators (>, >=, =, <, <=).
- ✅ Powerful Sorting: Sort results by any field (e.g., price, rating) in both ascending and descending order.
- ✅ Field Selection (Projection): Limit the fields returned in the response to reduce bandwidth.
- ✅ Smart Pagination: Easily navigate through large datasets using page and limit, with full metadata on total pages and results.
- ✅ Centralized Error Handling: Dedicated middleware for handling 404 routes and other application errors.
- ✅ Database Seeding: Includes a script to automatically populate the database with initial product data.
Store-API/
├── controllers/ # 🚦 API endpoint logic (filtering, sorting, pagination)
│ └── products.js
├── db/ # 💾 Database connection setup
│ └── connect.js
├── middleware/ # ⚙️ Express middleware functions (error handling, 404)
│ ├── error-handler.js
│ └── not-found.js
├── models/ # 📚 Mongoose data models (Product Schema)
│ └── product.js
├── routes/ # 🛣️ Express route definitions for API endpoints
│ └── productsRoutes.js
├── .env # 🔑 Environment variables (ignored by Git)
├── .gitignore # 🚫 Files/folders to exclude from Git tracking
├── app.js # 🚀 Main application entry point
├── package.json # 📦 Project metadata and dependencies
├── populate.js # 🌱 Script to seed the database
└── products.json # 📊 Sample product data
- app.js initializes the Express server, connects to the database, and sets up all middleware.
- An incoming request to /api/v1/products is directed by routes/productsRoutes.js.
- The corresponding function in controllers/products.js handles the core logic. It parses query parameters (sort, fields, numericFilters, etc.) to build a dynamic Mongoose query.
- The models/product.js schema defines the structure for product data in MongoDB.
- If any errors occur, they are passed to the middleware/error-handler.js for a consistent JSON response.
- The final result (a JSON object containing products and pagination data) is sent back to the client.
- Node.js: v14 or higher
- MongoDB: A local or cloud instance (e.g., MongoDB Atlas)
-
Clone the project:
git clone https://github.com/your-username/your-repo-name.git cd your-repo-name
-
Install dependencies:
npm install
-
Configure environment variables:
Create a.env
file in the project root and add your MongoDB connection string:MONGO_URI=your_mongodb_connection_string PORT=3000
-
Seed the Database:
Run the populate.js script to fill your database with sample products.node populate.js
To start the API server, run the following command:
npm start
The API will be live at http://localhost:3000 (or the port you specified).
The base URL for all endpoints is /api/v1
.
Retrieves a list of all products with extensive filtering, sorting, and pagination capabilities.
Query Parameters:
Parameter | Type | Description | Example |
---|---|---|---|
featured | Boolean | Filter by featured status | ?featured=true |
company | String | Filter by company name (ikea, liddy, caressa, marcos) | ?company=ikea |
name | String | Search by product name (case-insensitive) | ?name=table |
sort | String | Comma-separated fields to sort by (- for descending) | ?sort=-price,name |
fields | String | Comma-separated fields to include in the response | ?fields=name,price |
numericFilters | String | Numeric filters: [field][operator][value] | ?numericFilters=price>50,rating>=4.5 |
page | Number | The page number for pagination (Default: 1) | ?page=2 |
limit | Number | The number of items per page (Default: 10) | ?limit=5 |
Example Request:
curl "http://localhost:3000/api/v1/products?company=liddy&sort=-price&fields=name,price"
- Building and consuming complex RESTful APIs.
- Advanced database querying with MongoDB & Mongoose.
- Dynamic filtering, sorting, and pagination logic.
- Effective error handling strategies with custom middleware.
- Clean, modular, and scalable project architecture.
- Secure management of environment variables.
For questions, feedback, or suggestions:
GitHub: Shayan Sharifi
Email: shayansharificode@gmail.com
Feel free to fork and expand it into your own Store API system or use it as a learning resource!
MIT