A simple e-commerce backend built with MongoDB, Express, and Node.js.
Handles users, products, carts, and orders with JWT authentication and email verification.
- User Authentication
- Register, login, and email verification
- JWT-based route protection
- Products
- Admin can create, update, delete products
- All users can view products
- Supports filtering (category, price) and sorting (price, name, createdAt)
- Cart
- Add, update, remove, or clear items
- Cart is tied to a specific user
- Orders
- Checkout cart and create order
- Stock is updated on checkout
- Validation & Error Handling
- Joi validation for requests
- Global error handling middleware
- Environment Config
.envwith MongoDB, JWT, and email settings
- Security
- Admin routes protected by
isAdminmiddleware - Users cannot buy more than available stock
- Admin routes protected by
# Server
PORT=5000
NODE_ENV=development
# MongoDB
MONGO_URI=your_mongodb_connection_string
# JWT
JWT_SECRET_KEY=your_jwt_secret_key
# Email (Nodemailer)
EMAIL_USER=your_email@example.com
EMAIL_PASS=your_email_password
## API Routes
## Auth
### POST `/api/auth/register`
Register a new user.
### POST `/api/auth/login`
Login a user and return a JWT token.
### GET `/api/auth/verify-email/`
Verify a user's email using the verification code.
---
## Products
### GET `/api/products`
List all products. Supports optional query filters: category, minPrice, maxPrice, and sorting.
### GET `/api/products/:id`
Retrieve a single product by its ID.
### POST `/api/products` (Admin only)
Create a new product.
### PUT `/api/products/:id` (Admin only)
Update an existing product.
### DELETE `/api/products/:id` (Admin only)
Delete a product.
---
## Cart
### GET `/api/cart`
Retrieve the current user's cart.
### POST `/api/cart/add`
Add a product to the cart.
### PUT `/api/cart/update`
Update the quantity of a product in the cart.
### DELETE `/api/cart/delete`
Remove a product from the cart.
### DELETE `/api/cart/clear`
Clear all items from the cart.
---
## Orders
### POST `/api/orders/checkout`
Checkout the current cart, create an order, and update product stock.