A complete, production-ready backend API for a fully functional E-commerce platform. Built with Node.js, Express, and MongoDB, focusing on scalability, clean code architecture, and robust security measures.
This project implements advanced features like real-time stock management, secure cart operations, image processing, and comprehensive data validation.
- Runtime Environment: Node.js
- Framework: Express.js
- Database: MongoDB (with Mongoose ODM)
- Authentication JWT, Bcryptjs
- Validation: Joi
- File Uploads: Multer
- Email: Nodemailer
- Payment: Fawaterk API
- Dev Tools: ngrok (for local Webhook testing), Postman
- Security: Helmet, rateLimit, hpp, cors
- Performance: Compression
- Utilities: Slugify, express-async-handler
- JWT Authentication: Secure login and registration system using JSON Web Tokens.
- Role-Based Access Control (RBAC): Distinct roles for User and Admin (e.g., only admins can manage products/categories).
- Parameter Pollution: Protected against HPP (HTTP Parameter Pollution).
- Rate Limiting: Prevents brute-force attacks and DoS.
- CRUD Operations: Full management for Products, Categories, Subcategories, and Brands.
- Advanced Filtering: Filter by price, category, ratings, etc.
- Pagination & Sorting: Efficient data retrieval for large datasets.
- Image Handling:
- Multiple image uploads using Multer.
- Auto-Cleanup: System automatically deletes images from the server when a product or category is deleted to save storage space.
- Server-Side Calculations: Prices and totals are calculated on the backend to prevent manipulation.
- Stock Validation:
- Prevents adding items exceeding available stock.
- Automatically checks stock levels before updating quantities.
- Product Merging: Automatically merges duplicate items in the cart instead of creating new rows.
- Cart Cleanup: Automatically removes the cart if the last item is deleted.
- Real-time Average: Automatically calculates and updates the average rating and quantity of reviews for each product.
- Duplicate Prevention: Ensures a user can review a product only once.
- Global Error Handling: Centralized error controller for consistent API responses.
- Joi Validation: Strict validation for all incoming data (Body, Params, Queries).
- Friendly Error Messages: Clear messages for frontend developers (e.g., "Stock limit reached").
- Online Payment Gateway: Integrated with Fawaterk to accept secure online payments (Credit Cards, Wallets, etc.).
- Automated Order Management:
- Creates secure checkout sessions.
- Webhook Handling: Listens to payment gateway webhooks to automatically update order status (e.g., from
PendingtoPaid) in real-time. - Handles payment failures and cancellations gracefully.
-
Prerequisites
-
1-Clone the Repository and Install Dependencies
git clone https://github.com/AhmedSedkyy/nodejs-ecommerce-api
cd nodejs-ecommerce-api
npm install
- 2-Create a .env file
PORT=3000
DATABASE=mongodb://0.0.0.0:27017/e-commerce
JWT_SECRET=your_secret_key
FAWATERAK_BASE_URL=https://staging.fawaterk.com
FAWATERAK_API_TOKEN=Your FAWATERAK_API_TOKEN
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=465
EMAIL_USER=your_email@gmail.com
EMAIL_PASS=your_email_app_password
- 3-Run the Server
npm start
[Ahmed Sedky]