A secure RESTful E-Commerce Backend API built with Node.js, Express.js, and MongoDB following the MVC Architecture.
The API provides JWT Authentication, Role-Based Access Control (RBAC), product management, shopping cart functionality, and is designed as the backend for a modern e-commerce application.
The API enables users to browse products, manage shopping carts, and authenticate securely using JWT, while administrators can manage the product catalog through role-based access control (RBAC). It also demonstrates backend best practices such as error handling, database seeding, and modular project organization.
- As a shopper, I can register and log into my account.
- As a shopper, I can browse all available products.
- As a shopper, I can view product details.
- As a shopper, I can add products to my shopping cart.
- As a shopper, I can update product quantities in my cart.
- As a shopper, I can remove products from my cart.
- As a shopper, I can view my current shopping cart.
- As an administrator, I can create new products.
- As an administrator, I can update existing products.
- As an administrator, I can delete products.
- As an administrator, I can manage the product catalog securely.
- Register new users
- Login existing users
- JWT Authentication
- Password hashing using bcryptjs
- Role-Based Access Control
- Protected Routes
- Admin-only Product Management
- User-only Cart Management
- Get all products
- Get product by ID
- Create products (Admin)
- Update products (Admin)
- Delete products (Admin)
- Product Search
- Category Filtering
- Price Range Filtering
- Sorting
- Pagination
- Automatically create a cart for new users
- View cart
- Add products to cart
- Update product quantity
- Remove products from cart
- View a specific cart item
- MVC Architecture
- Centralized Error Handling
- Environment Variables
- MongoDB Atlas Support
- CORS Enabled
- Seed Script
- Postman Collection Included
- Node.js
- Express.js
- MongoDB
- Mongoose
- JWT
- bcryptjs
- dotenv
- CORS
back_end/
│
├── config/
│ └── db.js
│
├── controllers/
│ ├── authController.js
│ ├── cartController.js
│ └── productController.js
│
├── middleware/
│ ├── authMiddleware.js
│ ├── adminMiddleware.js
│ └── errorHandler.js
│
├── models/
│ ├── User.js
│ ├── Product.js
│ └── Cart.js
│
├── routes/
│ ├── authRoutes.js
│ ├── cartRoutes.js
│ └── productRoutes.js
│
├── services/
│
├── utils/
│ └── generateToken.js
│
├── postman demo/
│ ├── E-Commerce.postman_collection.json
│ └── E-Commerce Environment.postman_environment.json
│
├── seed.js
├── app.js
├── index.js
├── package.json
└── README.md
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/auth/register |
Register a new user |
| POST | /api/auth/login |
Login |
| GET | /api/auth/me |
Get current authenticated user |
| Method | Endpoint | Access |
|---|---|---|
| GET | /api/products |
Public |
| GET | /api/products/:id |
Public |
| POST | /api/products |
Admin |
| PATCH | /api/products/:id |
Admin |
| DELETE | /api/products/:id |
Admin |
| Query | Description |
|---|---|
search=laptop |
Search products by name or description |
category=Electronics |
Filter by category |
minPrice=100 |
Minimum price |
maxPrice=500 |
Maximum price |
sort=price |
Sort ascending |
sort=-price |
Sort descending |
page=2&limit=10 |
Pagination |
| Method | Endpoint |
|---|---|
| GET | /api/carts |
| POST | /api/carts |
| PATCH | /api/carts/:productId |
| DELETE | /api/carts/:productId |
| GET | /api/carts/:productId |
Protected routes require a valid JWT.
Authorization: Bearer YOUR_TOKENClone the repository
git clone https://github.com/Mohammed-Er/E-Commerce-API.gitInstall dependencies
npm installCreate a .env file
PORT=5000
MONGO_URI=your_mongodb_uri
JWT_SECRET=your_secretRun the project
npm run devor
npm startRun
npm run seedThis will:
- Delete existing Users
- Delete existing Products
- Delete existing Carts
- Insert sample Users
- Insert sample Products
Email: admin@test.com
Password: 123456
Email: user@test.com
Password: 123456
The repository includes a complete Postman Collection and Environment inside:
postman demo/
Import both files into Postman.
Environment Variables:
baseUrljwtproductId
Use an Admin JWT for product management endpoints.
Use a User JWT for shopping cart endpoints.
- Order Management System
- Wishlist
- Product Reviews
- Checkout System
- Stripe Integration
- React Frontend
- Docker Support
- Swagger API Documentation