This API provides endpoints to manage product inventory for an ecommerce platform. It utilizes Node.js with Express for the backend and MongoDB for data storage.
- Create, read, update, and delete (CRUD) operations for products.
- Update product quantities dynamically.
- Secure API endpoints with basic error handling.
Click here to visit the live site.
Before you begin, ensure you have met the following requirements:
- Node.js installed on your local machine. You can download it from nodejs.org.
- MongoDB installed and running locally or accessible via a remote server.
ecommerce-api
├── config # MongoDB configuration
│ └── mongoose.js
├── controllers # Controller logic
│ └── productController.js
├── images # Project Screenshots
├── models # Database models
│ └── productModel.js
├── routes # Route definitions
│ └── productRoutes.js
├── views # EJS views
│ ├── createProduct.ejs
│ ├── products.ejs
│ └── updateProduct.ejs
├── .env # Environment variables file
├── index.js # Express application setup
└── package.json
To set up this project locally, follow these steps:
-
Clone the repository
-
Install dependencies
npm install
- express
- mongoose
- ejs
- express-ejs-layouts
- dotenv
-
Set up environment variables
Create a
.env
file in the root directory andprovide the following variables:PORT=3000 MONGO_URI=mongodb://localhost:27017/ecommerce
Adjust
PORT
andMONGO_URI
as per your configuration. -
Start the server
npm start
The server should now be running on
http:/localhost:3000/products
. -
Explore the API Use tools like Postman or cURL to interact withthe API endpoints described below.
-
URL:
/products/create
-
Method: POST
-
Request Body:
{ "name": "laptop", "quantity": 10 }
-
Response:
{ "product": { "name": "laptop", "quantity": 10 } }
- URL:
/products
- Method: GET
- Response:
{ "products": [ { "id": "1", "name": "laptop", "quantity": 10 }, { "id": "2", "name": "camera", "quantity": 5 }, { "id": "3", "name": "smartwatch", "quantity": 8 } ] }
- URL:
/products/:id
- Method: DELETE
- Response:
{ "message": "Product deleted" }
You can either increment or decrement the product quantity
- URL:
/products/:<id>/update_quantity?number=<number>
- Method: POST
- Response:
{ "product": { "id": "1", "name": "laptop", "quantity": 20 }, "message": "Updated successfully" }
config/mongoose.js : Handles MongoDB connection using Mongoose.
controllers/productController.js : Contains the logic for handling requests to create, read, update, and delete products.
models/productModel.js : Defines the Mongoose schema and model for products.
routes/productRoutes.js : Defines the routes for product-related API endpoints and maps them to the controller functions.
views/ : Contains the EJS templates for the frontend interface.
Product List (views/products.ejs) : Displays all products in a table with options to update or delete each product.
Create Product Form (views/createProduct.ejs) : Form to create a new product.
Update Product Form (views/updateProduct.ejs) : Form to update the quantity of an existing product.
index.js : Sets up the Express server, connects to MongoDB, and defines the routes.
Styling : The frontend interface is styled using Bootstrap for a clean and responsive design.
This project was created by Ravikant Singh. Contributions via issues or pull requests are welcome!