Skip to content

a7medsa22/ecommerce-store

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

57 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ›’ Node.js E-commerce Store (Backend)

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.


โœ… Recent Features & Optimizations (summary)

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:

  1. Run local benchmarks (curl/Postman) and update the numbers with measured results.
  2. Monitor Redis hits and adjust TTLs in services/handlerFactors.js.
  3. If you want migration for legacy local images, add a migration script to re-upload to Cloudinary.

๐Ÿ“Œ Project Overview

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.


๐Ÿš€ Features

  • ๐Ÿ” 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)

๐Ÿงฐ Tech Stack

Core Technologies

  • Node.js - Runtime environment
  • Express.js - Web framework
  • MongoDB - Database
  • Mongoose - ODM for MongoDB

Authentication & Security

  • JWT - JSON Web Tokens for authentication
  • bcryptjs - Password hashing
  • express-rate-limit - Rate limiting
  • helmet - Security headers

File Handling

  • Multer - File upload middleware (memory storage)
  • Sharp - Image processing (optimized)
  • UUID - Unique file naming
  • Cloudinary - Remote image hosting (direct streaming)

Development & Testing

  • Jest - Testing framework
  • Supertest - HTTP testing
  • ESLint - Code linting
  • Prettier - Code formatting
  • Morgan - HTTP request logging

Utilities

  • dotenv - Environment variables
  • cors - Cross-origin resource sharing
  • compression - Response compression
  • slugify - URL-friendly slugs
  • nodemailer - Email sending

๐Ÿ› ๏ธ Installation & Setup

Prerequisites

  • Node.js (v14 or higher)
  • MongoDB (local or cloud instance)
  • Git

1. Clone the Repository

git clone <repository-url>
cd nodejs-ecommerce-stor

2. Install Dependencies

npm install

3. Environment Configuration

Create 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



4. Start the Server

Development Mode

npm start:dev

Production Mode

npm run start:prod

Testing

npm test

๐Ÿ“š API Endpoints

Authentication (/api/v1/auth/)

  • POST /signup - User registration
  • POST /login - User login
  • POST /forgotPassword - Request password reset
  • POST /resetCode - Verify reset code
  • POST /resetPassword - Reset password

Users (/api/v1/users/)

  • GET / - Get all users (Admin only)
  • GET /:id - Get user by ID
  • PUT /:id - Update user
  • DELETE /:id - Delete user (Admin only)
  • PUT /changePassword/:id - Change password
  • GET /getMe - Get current user profile
  • PUT /updateMe - Update current user profile (now supports updating the entire addresses array)
  • DELETE /deleteMe - Delete current user account

Orders (/api/v1/orders/)

  • 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 }
}

Categories (/api/v1/categories/)

  • GET / - Get all categories
  • POST / - Create category (Admin only)
  • GET /:id - Get category by ID
  • PUT /:id - Update category (Admin only)
  • DELETE /:id - Delete category (Admin only)

Subcategories (/api/v1/subcategories/)

  • GET / - Get all subcategories
  • POST / - Create subcategory (Admin only)
  • GET /:id - Get subcategory by ID
  • PUT /:id - Update subcategory (Admin only)
  • DELETE /:id - Delete subcategory (Admin only)

Brands (/api/v1/brands/)

  • GET / - Get all brands
  • POST / - Create brand (Admin only)
  • GET /:id - Get brand by ID
  • PUT /:id - Update brand (Admin only)
  • DELETE /:id - Delete brand (Admin only)

Products (/api/v1/products/)

  • GET / - Get all products (with filtering, sorting, pagination)
  • POST / - Create product (Admin only)
  • GET /:id - Get product by ID
  • PUT /:id - Update product (Admin only)
  • DELETE /:id - Delete product (Admin only)

Reviews (/api/v1/reviews/)

  • GET / - Get all reviews
  • POST / - Create review (Authenticated users)
  • GET /:id - Get review by ID
  • PUT /:id - Update review
  • DELETE /:id - Delete review

Wishlist (/api/v1/users/wishlist)

  • 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

Addresses (/api/v1/users/addresses)

  • 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)

๐Ÿ“ Project Structure

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

๐Ÿ“ฎ API Testing

Module Preview
Postman collection UI Photo Postman Collection

๐Ÿ“ฆ E-commerce API Collection

Easily explore and test the E-commerce API endpoints using the official Postman collection.
Click the button below to view it directly in Postman ๐Ÿ‘‡

View Document

๐Ÿ”ง Configuration

Environment Variables

  • PORT
  • NODE_ENV
  • DB_URL
  • JWT_SECRET_KEY
  • JWT_EXPIRES_IN
  • EMAIL_HOST
  • EMAIL_PORT
  • EMAIL_USER
  • EMAIL_PASSWORD

File Upload Configuration

  • JPEG, PNG, JPG
  • Max: 5MB
  • Sharp optimization
  • UUID for unique naming

๐Ÿงช Testing

npm test
npm test -- --watch
npm test -- --coverage

๐Ÿš€ Deployment

Production

npm run start:prod

Docker

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 8000
CMD ["npm", "run", "start:prod"]

๐Ÿค Contributing

  1. Fork
  2. Feature branch
  3. Commit
  4. Push
  5. Pull Request

๐Ÿ“ License

ISC License


๐Ÿ‘จโ€๐Ÿ’ป Author

Created with โค๏ธ for building robust e-commerce solutions.


๐Ÿ†˜ Support

  • Check existing issues
  • Create a new issue
  • Contact maintainers

Happy Coding! ๐Ÿš€

About

This project is a full-featured E-commerce backend built with Node.js and Express, designed to handle all core functionalities of an online store. It includes modules for products, categories, brands, users, authentication, orders, reviews, wishlist, and addresses. ๐Ÿ‘‰ The goal of this project is to demonstrate scalable backend architecture, RESTfu

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors