A full-stack e-commerce platform built with React, Node.js, Express, and MongoDB. This project includes features like product management, shopping cart, order processing, payment integration (VNPAY), loyalty program, and an admin dashboard.
- Tech Stack
- Project Structure
- Prerequisites
- Installation
- Configuration
- Running the Application
- API Documentation
- Deployment
- Scaling
- React 18: UI library
- React Router DOM: Routing
- Vite: Build tool and dev server
- Tailwind CSS: Styling
- Zustand: State management
- Axios: HTTP client
- Recharts: Data visualization
- Google OAuth: Social authentication
- Node.js: Runtime environment
- Express.js: Web framework
- MongoDB: Database
- Mongoose: ODM for MongoDB
- Socket.IO: WebSocket support
- JWT: Authentication tokens
- Bcrypt: Password hashing
- Nodemailer: Email service
- Zod: Schema validation
- VNPAY SDK: Payment gateway integration
- Docker: Containerization
- Docker Compose: Multi-container orchestration
- Nginx: Load balancer and reverse proxy
- MongoDB: Database
- MailHog: Email testing (development)
🧠 Gemini AI Chatbot Integration
A built-in helper chatbot is included in the frontend to assist users during shopping.
📌 Features ✔ Answer product questions automatically ✔ Works in real-time on UI ✔ API securely stored in .env
⚙️ CI/CD Pipeline (GitHub Actions)
Every time code is pushed to main, the workflow will:
- Checkout source
- Login Docker Hub
- Build backend image
- Build frontend image
- Push both to Docker Hub automatically
E-Commerce-Website/
├── backend/ # Backend API
│ ├── src/
│ │ ├── config/ # Configuration files
│ │ ├── controllers/ # Route controllers
│ │ ├── middleware/ # Custom middleware
│ │ ├── models/ # Mongoose models
│ │ ├── routes/ # API routes
│ │ ├── services/ # Business logic
│ │ └── server.js # Express server
│ ├── database/ # Database scripts
│ ├── Dockerfile # Backend Docker image
│ └── package.json
├── frontend/ # Frontend React app
│ ├── src/
│ │ ├── components/ # React components
│ │ ├── screens/ # Page components
│ │ ├── hooks/ # Custom hooks
│ │ ├── lib/ # Utilities
│ │ └── routes/ # Route definitions
│ ├── Dockerfile # Frontend Docker image
│ └── package.json
├── nginx/ # Nginx configuration
│ ├── nginx.conf # Nginx config
│ └── Dockerfile # Nginx Docker image
├── docker-compose.yml # Docker Compose configuration
├── .github
│ └── Workflows
│ └── deploy.yml # CI/CD Pipeline deployment
└── README.md
Before you begin, ensure you have the following installed:
- Node.js (v18 or higher)
- Docker and Docker Compose
- MongoDB (or use Docker)
- Git
git clone https://github.com/tranhohoangvu/E-Commerce-Website.git
cd E-Commerce-WebsiteCreate backend/.env file:
PORT=4000
NODE_ENV=development
# MongoDB
MONGODB_URI=mongodb://<USER>:<PASS>@<HOST>:27017/<DB_NAME>?authSource=admin
# JWT
JWT_SECRET=<YOUR_JWT_SECRET>
JWT_EXPIRES_IN=15m
JWT_REFRESH_SECRET=<YOUR_JWT_REFRESH_SECRET>
JWT_REFRESH_EXPIRES_IN=7d
# Google OAuth
GOOGLE_CLIENT_ID=<YOUR_GOOGLE_CLIENT_ID>
# Email (Development - MailHog)
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_FROM=noreply@ecommerce.com
# VNPAY (Sandbox)
VNP_TMN_CODE=<YOUR_VNP_TMN_CODE>
VNP_HASH_SECRET=<YOUR_VNP_HASH_SECRET>
VNP_URL=https://sandbox.vnpayment.vn/paymentv2/vpcpay.html
VNP_RETURN_URL=http://localhost:5173/payment/vnpay/return
VNP_IPN_URL=http://your-domain.com/api/payment/vnpay/ipn
# Frontend URL + CORS
FRONTEND_URL=http://localhost:5173
CORS_ORIGIN=http://localhost:5173
# (Optional) Gemini AI
GEMINI_API_KEY=<YOUR_GEMINI_API_KEY>Create frontend/.env file (optional, already in docker-compose.yml):
VITE_API_BASE_URL=http://localhost/api
VITE_GOOGLE_CLIENT_ID=<YOUR_GOOGLE_CLIENT_ID># Backend
cd backend
npm install
# Frontend
cd ../frontend
npm installThe MongoDB connection is configured in backend/.env. For Docker deployment, MongoDB is automatically set up via docker-compose.yml.
- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable Google+ API
- Create OAuth 2.0 credentials
- Add authorized redirect URIs
- Copy Client ID to
.envfile
- Register at VNPAY
- Get your Merchant Code (TMN Code) and Hash Secret
- Configure in
.envfile - Set Return URL and IPN URL in VNPAY dashboard
# Start all services (frontend, backend, nginx, mongo, mailhog)
docker compose up -d
# View logs
docker compose logs -f
# Stop all services
docker compose downAccess once the stack is up:
- Frontend: http://localhost
- Backend API: http://localhost/api (proxied via nginx)
- Swagger UI: http://localhost/api/docs
- MailHog UI: http://localhost:8025 (email testing)
- MongoDB: localhost:27017
POST /api/auth/signup- Register new userPOST /api/auth/login- Login userPOST /api/auth/google- Google OAuth loginPOST /api/auth/refresh- Refresh access tokenPOST /api/auth/logout- Logout userGET /api/auth/me- Get current user informationPOST /api/auth/forgot-password- Request password resetPOST /api/auth/reset-password- Reset password
GET /api/products- List products (with filters, search, pagination)GET /api/products/:id- Get product detailsPOST /api/products/:id/reviews- Create review (no auth required)GET /api/products/:id/reviews- List reviewsPOST /api/products/:id/ratings- Create/update rating (auth required)GET /api/products/:id/ratings/me- Get user's ratingPOST /api/products- Create product (admin only)PUT /api/products/:id- Update product (admin only)DELETE /api/products/:id- Delete product (admin only)
GET /api/cart- Get cartPOST /api/cart/add- Add item to cartPUT /api/cart/update- Update cart itemDELETE /api/cart/remove- Remove item from cart
POST /api/checkout/confirm- Confirm order and create paymentGET /api/orders/my- Get user ordersGET /api/orders/:id- Get order details
POST /api/payment/vnpay/create- Create VNPAY payment URLGET /api/payment/vnpay/return- Handle VNPAY returnPOST /api/payment/vnpay/ipn- Handle VNPAY IPN
GET /api/admin/dashboard/simple- Simple dashboard statsGET /api/admin/dashboard/advanced- Advanced dashboard with analyticsGET /api/admin/users- List usersGET /api/admin/orders- List ordersGET /api/admin/coupons- List couponsPOST /api/admin/coupons- Create coupon
GET /api/health- Health check endpoint
- Update environment variables in
docker-compose.yml - Build and start services:
docker-compose up -d --build- Check service status:
docker-compose psScale the backend service to multiple instances:
# Stop all services
docker-compose down
# Scale to 3 instances
docker-compose up -d --scale api=3
# Check status
docker-compose psNote: Remove port mapping for the api service in docker-compose.yml before scaling. Nginx will automatically load balance between instances.
-
Update environment variables for production:
- Set
MONGODB_URIto production MongoDB - Set
FRONTEND_URLto production domain - Set
VNP_RETURN_URLandVNP_IPN_URLto production URLs - Configure real SMTP server (Gmail, SendGrid, etc.)
- Set
-
Pull production images from Docker Hub:
# Pull images built by CI pipeline
docker pull <DOCKER_USERNAME>/ecom-backend:latest
docker pull <DOCKER_USERNAME>/ecom-frontend:latest
docker pull <DOCKER_USERNAME>/ecom-nginx:latest-
Configure Nginx for SSL/TLS (HTTPS)
-
Set up monitoring and logging
The application supports horizontal scaling:
- Stateless Architecture: JWT tokens, no session storage
- Load Balancing: Nginx round-robin load balancing
- Shared Database: MongoDB shared across instances
- Scalable Backend: Multiple API instances via Docker Compose
# Scale backend to 3 instances
docker-compose up -d --scale api=3
# Scale backend to 5 instances
docker-compose up -d --scale api=5
# View logs from all instances
docker-compose logs -f api- Add items to cart
- Proceed to checkout
- Select VNPAY payment method
- Use test card:
9704198526191432198(VNPAY sandbox) - OTP: Any 6 digits
- Register a new user
- Place an order
- Check MailHog UI at http://localhost:8025
# Development
npm run dev
# Seed database
npm run seed
# Create admin user
npm run create-admin
# Export products
npm run export:products
# Import products
npm run import:products# Development
npm run dev
# Build for production
npm run build
# Preview production build
npm run previewThis project is licensed under the MIT License.
The API documentation is available at:
- Swagger UI: http://localhost:4000/api/docs (Development)
- Swagger UI: http://localhost/api/docs (Production via Nginx)
- Swagger JSON: http://localhost:4000/api/docs.json
- Interactive API documentation
- Test endpoints directly from Swagger UI
- JWT authentication support (use "Authorize" button)
- Request/response schemas
- Example requests and responses
- Start the stack:
docker compose up -d - Open http://localhost/api/docs (proxied via nginx)
- Click "Authorize" button to add JWT token (Bearer token)
- Test endpoints directly from the UI
- GitHub Repository: https://github.com/tranhohoangvu/E-Commerce-Website
- Demo: https://youtu.be/UqbkiGuqmX0
- API Documentation: http://localhost:4000/api/docs (Swagger UI)