Modern, secure, and scalable RESTful API built for e-commerce applications using Node.js, Express, MongoDB, and modern best practices.
-
Secure Authentication
- JWT Access + Refresh Token rotation
- Email verification with secure hashed tokens
- Password change with session invalidation
- Role-based authorization (User / Admin)
-
Product Management
- CRUD operations (Admin only)
- Cloudinary image upload with size/format validation
- Advanced search, filtering, pagination & text search
- Discount price support
-
Category Management (nested categories support)
-
Order System
- Create orders with real-time stock deduction
- Multiple payment methods (COD + Card placeholder)
- Order status management (Admin)
- User order history
-
Reviews & Ratings
- Prevent duplicate reviews
- Automatic average rating & review count calculation
-
Admin Dashboard Analytics
- Overview stats (users, products, orders, revenue)
- Orders by status
- Monthly revenue breakdown
- Top selling products
-
Developer Experience
- Joi validation middleware
- Global error handling & 404
- Environment-based email (Ethereal for dev, Gmail for prod)
- Clean MVC-like architecture (Controllers / Services / Models)
- Comprehensive logging & health check endpoint
- Runtime: Node.js + Express
- Database: MongoDB (Mongoose)
- Authentication: JWT + Refresh Tokens
- Image Storage: Cloudinary + Multer
- Email: Nodemailer (Ethereal dev / Gmail prod)
- Validation: Joi
- Security: bcrypt, helmet (recommended), rate limiting (recommended)
- Other: slugify, crypto, aggregation pipelines
.
βββ app.js # Application entry point
βββ package.json # Project dependencies
βββ README.md # Project documentation
βββ LICENSE # MIT License
βββ .env.example # Environment variables template
βββ Ecommerce-Backend-API.postman_collection.json # Postman collection
βββ src/
βββ config/ # Configuration files
β βββ cloudinary.js # Cloudinary setup
β βββ DB.js # MongoDB connection
β βββ nodemailer.js # Email configuration
βββ controllers/ # Route handlers & business logic
β βββ admin.controller.js # Admin analytics endpoints
β βββ auth.controller.js # Authentication logic
β βββ category.controller.js # Category operations
β βββ order.controller.js # Order management
β βββ product.controller.js # Product operations
β βββ review.controller.js # Review operations
βββ middlewares/ # Express middlewares
β βββ auth.middleware.js # JWT authentication
β βββ role.middleware.js # Role-based access control
β βββ upload.middleware.js # Image upload handling
β βββ validation.js # Request validation
βββ models/ # Database schemas (Mongoose)
β βββ Category.js
β βββ Order.js
β βββ Product.js
β βββ RefreshToken.js
β βββ Review.js
β βββ User.js
βββ routes/ # API route definitions
β βββ admin.routes.js
β βββ auth.routes.js
β βββ category.routes.js
β βββ order.routes.js
β βββ product.routes.js
β βββ review.routes.js
βββ services/ # Business logic & database operations
β βββ admin.service.js
β βββ auth.service.js
β βββ category.service.js
β βββ email.service.js
β βββ order.service.js
β βββ product.service.js
β βββ review.service.js
βββ utils/ # Utility functions
β βββ emailsTemplates.js # Email HTML templates
β βββ tokens.js # Token generation utilities
βββ validator/ # Request validation schemas
βββ auth.validator.js # Joi validation schemas
- Node.js β₯ 18
- MongoDB (local or Atlas)
- Cloudinary account
- Gmail (for production emails β use App Password)
# Clone the repository
git clone https://github.com/MinaMalakH/Ecommerce-Backend-API.git
cd Ecommerce-Backend-API
# Install dependencies
npm install
# Create .env file (use .env.example as template)
cp .env.example .env
# Fill in your environment variables
# Important ones:
# MONGODB_URI
# JWT_ACCESS_SECRET
# JWT_REFRESH_SECRET
# CLOUDINARY_CLOUD_NAME / API_KEY / API_SECRET
# EMAIL_USER / EMAIL_PASS (for production)
# FRONTEND_URL
# Start development server
npm run devBase URL: http://localhost:5000/api
| Method | Endpoint | Description | Auth |
|---|---|---|---|
| GET | /health |
Server health check | β |
| Method | Endpoint | Description | Auth |
|---|---|---|---|
| POST | /auth/register |
Register new user | β |
| POST | /auth/login |
Login user | β |
| POST | /auth/refresh |
Refresh access token | β |
| POST | /auth/logout |
Logout user | β |
| GET | /auth/verify-email |
Verify email with token | β |
| GET | /auth/resend-verification |
Resend verification email | β |
| GET | /auth/me |
Get current user profile | β |
| PATCH | /auth/change-password |
Change user password | β |
| Method | Endpoint | Description | Auth | Role |
|---|---|---|---|---|
| POST | /category |
Create category | β | Admin |
| GET | /category |
Get all categories | β | - |
| GET | /category/:id |
Get category by ID | β | - |
| PATCH | /category/:id |
Update category | β | Admin |
| DELETE | /category/:id |
Delete category | β | Admin |
| Method | Endpoint | Description | Auth | Role |
|---|---|---|---|---|
| POST | /product |
Create product (with image) | β | Admin |
| GET | /product |
Get all products (paginated, searchable) | β | - |
| GET | /product/:id |
Get product by ID | β | - |
| PATCH | /product/:id |
Update product | β | Admin |
| DELETE | /product/:id |
Delete product | β | Admin |
Query Parameters for GET /product:
page- Page number (default: 1)limit- Items per page (default: 10)search- Search by name or description
| Method | Endpoint | Description | Auth | Role |
|---|---|---|---|---|
| POST | /order |
Create order | β | User |
| GET | /order/me |
Get user's orders | β | User |
| GET | /order |
Get all orders | β | Admin |
| PATCH | /order/:id |
Update order status | β | Admin |
| Method | Endpoint | Description | Auth | Role |
|---|---|---|---|---|
| POST | /review/:productId |
Create/update product review | β | User |
| Method | Endpoint | Description | Auth | Role |
|---|---|---|---|---|
| GET | /admin/analytics/overview |
Get overview stats (users, products, orders, revenue) | β | Admin |
| GET | /admin/analytics/order-by-status |
Get orders grouped by status | β | Admin |
| GET | /admin/analytics/monthly-revenue |
Get monthly revenue breakdown | β | Admin |
| GET | /admin/analytics/top-products |
Get top selling products | β | Admin |
Legend:
- β = Requires authentication
- β = Public endpoint
A complete Postman collection is included: Ecommerce-Backend-API.postman_collection.json
- Open Postman
- Click Import β Upload Files (or drag & drop)
- Select
Ecommerce-Backend-API.postman_collection.json - Set environment variables:
base_url=http://localhost:5000(or your API URL)access_token= Your JWT token (obtained after login)
Create a .env file in the root directory (see .env.example):
# Server
NODE_ENV=development
PORT=5000
# Database
MONGODB_URI=mongodb://localhost:27017/ecommerce
# JWT
JWT_ACCESS_SECRET=your_access_secret_key_here
JWT_ACCESS_EXPIRY=15m
JWT_REFRESH_SECRET=your_refresh_secret_key_here
JWT_REFRESH_EXPIRY=7d
# Cloudinary
CLOUDINARY_CLOUD_NAME=your_cloudinary_name
CLOUDINARY_API_KEY=your_cloudinary_key
CLOUDINARY_API_SECRET=your_cloudinary_secret
# Email (Development - Ethereal)
EMAIL_SERVICE=gmail
EMAIL_USER=your_gmail@gmail.com
EMAIL_PASS=your_app_password
# Frontend
FRONTEND_URL=http://localhost:3000This project is licensed under the MIT License - see the LICENSE file for details.
Mina Malak
- GitHub: @MinaMalakH
- Project: Ecommerce-Backend-API
Contributions are welcome! Feel free to open issues or submit pull requests.
If you found this project helpful, please give it a β on GitHub!