A practical backend API built with Node.js, Express, MongoDB, and Mongoose to demonstrate important database engineering concepts through a complete product API.
- MongoDB database integration
- Mongoose schema and model
- CRUD operations
- Schema validation
- Database indexes
- Text search
- Category filtering
- Price range filtering
- Stock filtering
- Pagination
- Sorting
- Soft delete
- Restore deleted records
- Seed data
- API versioning
- Centralized error handling
- Graceful server shutdown
- Environment configuration
- Health check endpoint
- API testing with
requests.http
- Node.js
- Express
- MongoDB
- Mongoose
- Nodemon
GET /GET /api/v1/healthGET /api/v1/productsGET /api/v1/products/:idThis endpoint returns active products only. Soft-deleted products are omitted, matching the default product list behavior.
POST /api/v1/productsPUT /api/v1/products/:idDELETE /api/v1/products/:idPATCH /api/v1/products/:id/restoreGET /api/v1/products?search=keyboardGET /api/v1/products?page=1&limit=5GET /api/v1/products?category=AccessoriesGET /api/v1/products?minPrice=2000&maxPrice=7000GET /api/v1/products?inStock=trueGET /api/v1/products?sort=price&order=ascGET /api/v1/products?includeDeleted=trueCreate a .env file using .env.example:
PORT=1198
MONGO_URI=mongodb://127.0.0.1:27017/database_engineering_apiThe .env file is ignored by Git, while .env.example documents the required environment variables.
Every JSON response starts with apiUrl, and each request is logged with its method, path, and status code.
The health endpoint returns HTTP 503 when MongoDB is disconnected. Unexpected server errors return a generic message; details are written to the server log.
npm installnpm run seedThis clears the existing product collection and inserts the included demonstration products.
npm run devnpm start1198
database-engineering-api/
|-- scripts/
| `-- seed.js
|-- src/
| |-- config/database.js
| |-- controllers/product.controllers.js
| |-- middlewares/
| | |-- errorHandler.middleware.js
| | `-- notFound.middleware.js
| |-- models/product.model.js
| |-- routes/product.routes.js
| |-- validators/product.validators.js
| |-- app.js
| `-- server.js
|-- .env.example
|-- .gitignore
|-- package.json
|-- requests.http
`-- README.md
Ashish Ranjan
- Portfolio: https://www.ashishranjan.net
- GitHub: https://github.com/a2rp
- CodePen: https://codepen.io/ash1198
- LinkedIn: https://www.linkedin.com/in/aashishranjan
- Facebook: https://www.facebook.com/theash.ashish/
- YouTube: https://www.youtube.com/@ashishranjan-ashz?sub_confirmation=1
- Email: mailto:ash.ranjan09@gmail.com
- Support: https://a2rp-donation-page.netlify.app/
- Buy Me A Coffee: https://buymeacoffee.com/a2rp
- Patreon: https://patreon.com/a2rp
MIT License