This is an eCommerce API built with Node.js, Express.js, JWT for authentication, and Mongoose for MongoDB.
- User authentication with JWT (Login and Register)
- CRUD operations for products
- Cart management
- Secure endpoints with JWT middleware
- Node.js
- Express.js
- JSON Web Token (JWT)
- Mongoose
- MongoDB
- Node.js installed
- MongoDB installed
. Install the dependencies:
npm install- Create a
config.envfile in the root directory and add the following variables:
PORT=5000
MONGO_URI=your_mongo_db_connection_string
JWT_SECRET=your_jwt_secret
- Start the server:
npm startThe server will start on the port defined in your config.env file (default is 5000).
- Register:
POST /api/auth/register - Login:
POST /api/auth/login
- Get all products:
GET /api/products - Get product by ID:
GET /api/products/:id - Create product:
POST /api/products - Update product:
PUT /api/products/:id - Delete product:
DELETE /api/products/:id
- Get user's cart:
GET /api/cart - Add item to cart:
POST /api/cart - Update item in cart:
PUT /api/cart/:itemId - Remove item from cart:
DELETE /api/cart/:itemId - Clear cart:
DELETE /api/cart
ecommerce-api/
├── config/
│ └── db.js
| └── config.env
├── controllers/
│ ├── user.js
│ ├── product.js
│ └── cart.js
├── middleware/
│ ├── requireauth.js
├── models/
│ ├── User.js
│ ├── Product.js
│ └── Cart.js
├── routes/
│ ├── user.js
│ ├── product.js
│ └── cart.js
├── .gitignore
├── package.json
├── server.js
└── README.md