Skip to content

sabbirhosen44/Ecommerce-Backend-Project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

9 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

E-Commerce Backend API

A RESTful API for an e-commerce platform built with Node.js, Express, and MongoDB. This backend API powers a complete e-commerce platform with user authentication, product management, shopping cart, order processing, and integrated payment system.

Node.js Express MongoDB

โœจ Key Features

  • ๐Ÿ” JWT Authentication - Secure user registration and login
  • ๐Ÿ‘ค User Management - Profile updates, password change, OTP-based password reset
  • ๐Ÿ“ฆ Product Catalog - CRUD operations with search and category filtering
  • ๐Ÿ–ผ๏ธ Image Upload - Multi-image support via Cloudinary
  • ๐Ÿ›’ Shopping Cart - Add, update, remove items
  • ๐Ÿ’ณ Payment Integration - Stripe payment gateway
  • ๐Ÿ“‹ Order Management - Complete order lifecycle tracking
  • ๐Ÿ”ง Admin Panel - Product, category, and order management
  • ๐Ÿ“ง Email Service - OTP verification via Nodemailer
  • ๐Ÿ›ก๏ธ Security - Password hashing, secure cookies, CORS

๐Ÿ› ๏ธ Tech Stack

Category Technologies
๐ŸŒ Backend
๐Ÿ—„๏ธ Database
๐Ÿ” Authentication
๐Ÿ’ณ Payment
โ˜๏ธ Cloud & File Handling
๐Ÿ“ง Communication
๐Ÿ›ก๏ธ Security & Middleware
โœ… Validation

๐Ÿ“ Project Structure

โ”œโ”€โ”€ controllers/          # Business logic
โ”‚   โ”œโ”€โ”€ user.js          # Authentication & user management
โ”‚   โ”œโ”€โ”€ product.js       # Product & category operations
โ”‚   โ””โ”€โ”€ order.js         # Order processing
โ”œโ”€โ”€ models/              # MongoDB schemas
โ”‚   โ”œโ”€โ”€ user.js
โ”‚   โ”œโ”€โ”€ product.js
โ”‚   โ”œโ”€โ”€ order.js
โ”‚   โ””โ”€โ”€ category.js
โ”œโ”€โ”€ routes/              # API endpoints
โ”‚   โ”œโ”€โ”€ user.js
โ”‚   โ”œโ”€โ”€ product.js
โ”‚   โ””โ”€โ”€ order.js
โ”œโ”€โ”€ middlewares/         # Custom middleware
โ”‚   โ”œโ”€โ”€ auth.js          # Authentication & authorization
โ”‚   โ”œโ”€โ”€ error.js         # Error handling
โ”‚   โ””โ”€โ”€ multer.js        # File upload
โ”œโ”€โ”€ utils/               # Helper functions
โ”‚   โ”œโ”€โ”€ features.js      # Utility functions
โ”‚   โ””โ”€โ”€ error.js         # Custom error class
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ database.js      # MongoDB connection
โ”œโ”€โ”€ postman/             # API testing
โ”‚   โ”œโ”€โ”€ Ecommerce-Backend.postman_collection.json
โ”‚   โ””โ”€โ”€ POSTMAN-SETUP.md        # Postman setup guide
โ”œโ”€โ”€ .env                 # Environment variables
โ”œโ”€โ”€ app.js               # Express configuration
โ””โ”€โ”€ README.md            # Project documentation
โ””โ”€โ”€ package.json         # Dependencies

๐Ÿš€ Quick Start

Prerequisites

  • Node.js (v14+)
  • MongoDB
  • Cloudinary account
  • Stripe account

Installation

  1. Clone the repository
git clone <repository-url>
cd ecommerce-backend
  1. Install dependencies
npm install
  1. Configure environment variables

Create data/config.env:

PORT=5000
NODE_ENV=Development

MONGO_URI=your_mongodb_uri

JWT_SECRET=your_jwt_secret

CLOUDINARY_NAME=your_cloudinary_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret

STRIPE_API_SECRET=your_stripe_secret

SMTP_HOST=smtp.gmail.com
SMTP_PORT=465
SMTP_USER=your_email@gmail.com
SMTP_PASS=your_email_password

FRONTEND_URI_1=http://localhost:3000
FRONTEND_URI_2=http://localhost:5173
  1. Run the server
npm run dev

Server runs on http://localhost:4000

๐Ÿ“ฎ API Testing with Postman

We provide a complete Postman collection for easy API testing and integration.

Quick Setup

  • Import the collection into Postman from postman/Ecommerce-Backend.postman_collection.json
  • Set up environment variables (BASE_URL, ACCESS_TOKEN)
  • Start testing all endpoints!

๐Ÿ‘‰ View Complete Postman Setup Guide POSTMAN-SETUP.md

๐Ÿ“š API Endpoints

Authentication

POST   /api/v1/user/new              # Register
POST   /api/v1/user/login            # Login
GET    /api/v1/user/logout           # Logout
GET    /api/v1/user/me               # Get profile
PUT    /api/v1/user/updateprofile    # Update profile
PUT    /api/v1/user/changepassword   # Change password
PUT    /api/v1/user/updatepic        # Update avatar
POST   /api/v1/user/forgetpassword   # Request OTP
PUT    /api/v1/user/resetpassword    # Reset with OTP

Products

GET    /api/v1/product/all           # Get all products
GET    /api/v1/product/admin         # Admin dashboard
GET    /api/v1/product/single/:id    # Get single product
POST   /api/v1/product/new           # Create product (Admin)
PUT    /api/v1/product/single/:id    # Update product (Admin)
POST   /api/v1/product/images/:id    # Add image (Admin)
DELETE /api/v1/product/images/:id    # Delete image (Admin)
DELETE /api/v1/product/single/:id    # Delete product (Admin)

Categories

GET    /api/v1/product/categories    # Get all categories
POST   /api/v1/product/category      # Add category (Admin)
DELETE /api/v1/product/category/:id  # Delete category (Admin)

Orders

POST   /api/v1/order/payment         # Create payment intent
POST   /api/v1/order/new             # Place order
GET    /api/v1/order/my              # Get my orders
GET    /api/v1/order/single/:id      # Get order details
GET    /api/v1/order/admin           # Get all orders (Admin)
PUT    /api/v1/order/single/:id      # Process order (Admin)

๐Ÿ“Š Database Models

User

  • Email, password (hashed), name, address details
  • Avatar with Cloudinary integration
  • Role-based access (user/admin)
  • OTP for password reset

Product

  • Name, description, price, stock
  • Multiple images (Cloudinary)
  • Category reference
  • Timestamps

Order

  • Shipping information
  • Order items with product references
  • Payment details (COD/Online)
  • Status tracking (Preparing โ†’ Shipped โ†’ Delivered)
  • Automatic stock updates

Category

  • Simple category name

๐Ÿ”’ Security Features

  • โœ… JWT-based authentication
  • โœ… Password hashing with bcrypt (10 rounds)
  • โœ… HTTP-only secure cookies
  • โœ… CORS configuration
  • โœ… Input validation
  • โœ… OTP-based password reset
  • โœ… Protected routes with middleware
  • โœ… Admin authorization checks

๐ŸŽ“ Learning Outcomes

This project demonstrates:

  • RESTful API design principles
  • Authentication and authorization
  • Database modeling with relationships
  • Payment gateway integration
  • Cloud storage implementation
  • Email service integration
  • Error handling best practices
  • Middleware architecture
  • Secure coding practices

๐Ÿ“ Environment Setup

The project uses separate environment configurations:

  • Development - Local testing with detailed errors
  • Production - Optimized for deployment

๐Ÿงช Testing

Use tools like Postman, Thunder Client, or cURL:

# Test server status
curl http://localhost:5000/

# Register user
curl -X POST http://localhost:5000/api/v1/user/new \
  -H "Content-Type: application/json" \
  -d '{"name":"John","email":"john@example.com","password":"123456"}'

๐Ÿš€ Deployment Ready

  • Environment-based configuration
  • MongoDB Atlas compatible
  • Cloudinary cloud storage
  • Stripe production keys support
  • CORS configured for frontend
  • Ready for Heroku, Railway, Render, etc.

๐Ÿ“ˆ Performance Features

  • Mongoose query optimization
  • Efficient image storage with Cloudinary
  • Proper indexing on database
  • Async/await for non-blocking operations
  • Error handling prevents crashes

๐ŸŽจ API Response Format

Success Response:

{
  "success": true,
  "message": "Operation successful",
  "data": {}
}

Error Response:

{
  "success": false,
  "message": "Error description"
}

๐Ÿ“ฆ Dependencies

{
  "express": "^4.18.2",
  "mongoose": "^6.7.2",
  "jsonwebtoken": "^8.5.1",
  "bcrypt": "^5.1.0",
  "cloudinary": "^1.32.0",
  "stripe": "^10.17.0",
  "nodemailer": "^6.8.0",
  "multer": "^1.4.5-lts.1",
  "validator": "^13.7.0",
  "cookie-parser": "^1.4.6",
  "cors": "^2.8.5",
  "dotenv": "^16.0.3"
}

๐Ÿ”ฎ Future Enhancements

  • Product reviews and ratings
  • Advanced search with filters
  • Pagination for large datasets
  • Admin analytics dashboard
  • Real-time notifications
  • Order cancellation and refunds
  • Coupon/discount system
  • Product recommendations

โญ If you found this project helpful, please consider giving it a star!

About

Full-featured Ecommerce Backend with Node.js, MongoDB, and Stripe Payment Integration.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors