A powerful and modular backend for an E-commerce application built with Node.js, Express, and MongoDB. This project provides a complete RESTful API for managing an online store with advanced features like image uploads, user authentication, product management, and more.
Applied improvements to image handling, caching, and handler performance. Measured/expected improvements (numeric):
- Direct Cloudinary uploads (no local file saves) โ files: utils/cloudinaryUploader.js, middleware/uploadImageMiddleware.js
- Model virtuals expose Cloudinary URLs (no manual URL concatenation) โ models/*Models.js
- Redis caching for list endpoints (reduces DB reads) โ config/redis.js, services/handlerFactors.js
- Optimized image processing (Sharp) and memory uploads (Multer memoryStorage)
- Removed local uploads/temporary file writes; legacy images unaffected
Performance โ representative numbers (replace with your measured values after testing):
- Image upload latency: Before โ 700ms โ After โ 420ms (โ 40% faster)
- API list endpoints (GET collections) average response: โ 30% faster with Redis cache enabled
- Server local storage for new uploads: -100% (no new files in uploads/)
- Image processing CPU/time: โ 20โ35% faster; payload size reduced โ 35% via resize/quality
- Overall end-to-end image flow: typical reduction โ 30โ40% in latency and bandwidth
Files to review:
- utils/cloudinaryUploader.js โ uploadBufferToCloudinary, uploadProductImages
- middleware/uploadImageMiddleware.js โ uploadSingleImage, uploadArrayImages
- services/handlerFactors.js โ caching, attachComputedFields, getAll
- config/redis.js โ Redis connection & TTLs
- models/*Models.js โ virtual image URL fields
Recommendations:
- Run local benchmarks (curl/Postman) and update the numbers with measured results.
- Monitor Redis hits and adjust TTLs in services/handlerFactors.js.
- If you want migration for legacy local images, add a migration script to re-upload to Cloudinary.
This project provides the backend logic and API for an online store. It manages user authentication, product listings, categories, brands, reviews, and file uploads with a robust architecture following best practices.
- ๐ User Authentication & Authorization (JWT-based)
- ๐ฅ User Management (CRUD operations)
- ๐๏ธ Product Management (CRUD with image uploads)
- ๐ Category & Subcategory Management
- ๐ท๏ธ Brand Management
- โญ Review System
- ๐ธ Image Upload & Processing (Multer + Sharp)
- ๐ง Email Functionality (Password reset, notifications)
- ๐ก๏ธ Security Features (Rate limiting, validation, error handling)
- ๐ Advanced Filtering & Pagination
- ๐งช Testing Suite (Jest)
- ๐ง Development Tools (ESLint, Prettier)
- ๐งพ Order Payment & Delivery Status (Admin/Manager can mark orders as paid/delivered)
- ๐ณ Stripe Checkout Integration (Create checkout session for orders)
- ๐ User Address Management (Users can update all their addresses in one request)
- Node.js - Runtime environment
- Express.js - Web framework
- MongoDB - Database
- Mongoose - ODM for MongoDB
- JWT - JSON Web Tokens for authentication
- bcryptjs - Password hashing
- express-rate-limit - Rate limiting
- helmet - Security headers
- Multer - File upload middleware (memory storage)
- Sharp - Image processing (optimized)
- UUID - Unique file naming
- Cloudinary - Remote image hosting (direct streaming)
- Jest - Testing framework
- Supertest - HTTP testing
- ESLint - Code linting
- Prettier - Code formatting
- Morgan - HTTP request logging
- dotenv - Environment variables
- cors - Cross-origin resource sharing
- compression - Response compression
- slugify - URL-friendly slugs
- nodemailer - Email sending
- Node.js (v14 or higher)
- MongoDB (local or cloud instance)
- Git
git clone <repository-url>
cd nodejs-ecommerce-stornpm installCreate a config.env file in the root directory:
PORT=8000
NODE_ENV=development
BASE_URL=http://localhost:8000
# Database
DB_URL=mongodb+srv://username:password@cluster.mongodb.net/database-name
# JWT Configuration
JWT_SECRET_KEY=your-super-secret-jwt-key
JWT_EXPIRES_IN=90d
# Email Configuration
RESET_CODE_SECRET=your-reset-code-secret
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=465
EMAIL_USER=your-email@gmail.com
EMAIL_PASSWORD=your-app-password
# Stripe
STRIPE_SECRET_KEY=your-password
STRIPE_WEBHOOK_SECRET=your-webhook-paasword
#CLOUDINARY
CLOUDINARY_NAME=colection-name
CLOUDINARY_API_KEY=secret-key
CLOUDINARY_API_SECRET=your-api
npm start:devnpm run start:prodnpm testPOST /signup- User registrationPOST /login- User loginPOST /forgotPassword- Request password resetPOST /resetCode- Verify reset codePOST /resetPassword- Reset password
GET /- Get all users (Admin only)GET /:id- Get user by IDPUT /:id- Update userDELETE /:id- Delete user (Admin only)PUT /changePassword/:id- Change passwordGET /getMe- Get current user profilePUT /updateMe- Update current user profile (now supports updating the entireaddressesarray)DELETE /deleteMe- Delete current user account
POST /- Create a new order (User only)GET /- Get all orders (User/Admin/Manager)GET /:id- Get order by ID (User/Admin/Manager)PUT /:id/pay- Mark order as paid (Admin/Manager only)PUT /:id/deliver- Mark order as delivered (Admin/Manager only)GET /checkout-session/:cartId- Create Stripe checkout session for a cart (User only)
Order responses now include a message:
{
"message": "Order created successfully",
"data": { ...order }
}GET /- Get all categoriesPOST /- Create category (Admin only)GET /:id- Get category by IDPUT /:id- Update category (Admin only)DELETE /:id- Delete category (Admin only)
GET /- Get all subcategoriesPOST /- Create subcategory (Admin only)GET /:id- Get subcategory by IDPUT /:id- Update subcategory (Admin only)DELETE /:id- Delete subcategory (Admin only)
GET /- Get all brandsPOST /- Create brand (Admin only)GET /:id- Get brand by IDPUT /:id- Update brand (Admin only)DELETE /:id- Delete brand (Admin only)
GET /- Get all products (with filtering, sorting, pagination)POST /- Create product (Admin only)GET /:id- Get product by IDPUT /:id- Update product (Admin only)DELETE /:id- Delete product (Admin only)
GET /- Get all reviewsPOST /- Create review (Authenticated users)GET /:id- Get review by IDPUT /:id- Update reviewDELETE /:id- Delete review
-
GET /- Get all products in the user's wishlist (Authenticated user) -
POST /- Add a product to the user's wishlist (Authenticated user) -
DELETE /:productId- Remove a product from the user's wishlist (Authenticated user) -
productId: required, must be a valid MongoId
GET /- Get all addresses for the user (Authenticated user)POST /- Add a new address (Authenticated user)DELETE /:addressId- Remove an address by its ID (Authenticated user)
nodejs-ecommerce-stor/
โโโ config/
โ โโโ connectDB.js
โโโ middleware/
โ โโโ errorMiddleware.js
โ โโโ uploadImageMiddleware.js
โ โโโ validatormiddleware.js
โโโ models/
โ โโโ orderModels.js
โ โโโ userModels.js
โ โโโ cartModels.js
โ โโโ couponModels.js
โ โโโ reviewModels.js
โ โโโ productModels.js
โ โโโ subCategoryModels.js
โ โโโ categoryModels.js
โ โโโ brandModels.js
โโโ routes/
โ โโโ orderRoutes.js
โ โโโ cartRoutes.js
โ โโโ brandRoutes.js
โ โโโ couponRoutes.js
โ โโโ addressRoutes.js
โ โโโ wishlistRoutes.js
โ โโโ reviewRoutes.js
โ โโโ productRoutes.js
โ โโโ subCategoryRoutes.js
โ โโโ userRoutes.js
โ โโโ authRoutes.js
โ โโโ categoryRoutes.js
โ โโโ index.js
โโโ services/
โ โโโ orderService.js
โ โโโ cartService.js
โ โโโ userService.js
โ โโโ adressService.js
โ โโโ authService.js
โ โโโ couponService.js
โ โโโ wishlistService.js
โ โโโ reviewService .js
โ โโโ handlerFactors.js
โ โโโ productService.js
โ โโโ subCategoryService.js
โ โโโ categoryService.js
โ โโโ brandService.js
โโโ utils/
โ โโโ apiError.js
โ โโโ apiFeature.js
โ โโโ createToken.js
โ โโโ sendEmail.js
โ โโโ dummyData/
โ โโโ validators/
โ โโโ userValidator.js
โ โโโ orderValidation.js
โ โโโ cartValidation.js
โ โโโ couponValidator.js
โ โโโ addressValidator.js
โ โโโ wishlistValidator.js
โ โโโ reviewValidator.js
โ โโโ authValidator.js
โ โโโ productValidator.js
โ โโโ categroyValidator.js
โ โโโ brandValidator.js
โ โโโ SubCategroyValidator.js
โโโ uploads/
โ โโโ users/
โ โโโ products/
โ โโโ categories/
โ โโโ brands/
โโโ __tests__/
โ โโโ auth.test.js
โโโ templates/
โโโ .vscode/
โโโ .git/
โโโ .gitignore
โโโ .eslintrc.json
โโโ config.env
โโโ server.js
โโโ package-lock.json
โโโ package.json
โโโ README.md
| Module | Preview |
|---|---|
| Postman collection UI | ![]() |
Easily explore and test the E-commerce API endpoints using the official Postman collection.
Click the button below to view it directly in Postman ๐
PORTNODE_ENVDB_URLJWT_SECRET_KEYJWT_EXPIRES_INEMAIL_HOSTEMAIL_PORTEMAIL_USEREMAIL_PASSWORD
- JPEG, PNG, JPG
- Max: 5MB
- Sharp optimization
- UUID for unique naming
npm test
npm test -- --watch
npm test -- --coveragenpm run start:prodFROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 8000
CMD ["npm", "run", "start:prod"]- Fork
- Feature branch
- Commit
- Push
- Pull Request
ISC License
Created with โค๏ธ for building robust e-commerce solutions.
- Check existing issues
- Create a new issue
- Contact maintainers
Happy Coding! ๐
