A full-stack e-commerce application built with React, Node.js, Express, and MongoDB. This repository is a monorepo containing both the frontend storefront and the backend API.
This README is written so that a developer who did not build the project can understand the system and run it step by step.
- Customer storefront with product browsing, filtering, cart, and checkout.
- Authentication with secure login/register, JWT access and refresh tokens.
- User profile editing and order history.
- Admin dashboard for managing products, orders, and users.
- Light/dark theme toggle.
- Security middleware protecting the backend API.
The system is split into two main parts:
-
Backend (
backend/)- Express API server.
- MongoDB database connection.
- Endpoints for auth, products, cart, orders, and admin actions.
- Security middleware for CORS, rate limiting, headers, and input sanitization.
-
Frontend (
frontend/)- React application using Vite.
- Redux Toolkit for global state.
- Axios for communicating with the backend.
- Pages for customers and admin users.
The repository is organized as a monorepo with separate frontend and backend applications.
backend/
βββ server.js # Application entry point
βββ src/
β βββ app.js # Express app and middleware configuration
β βββ config/
β β βββ database.js # MongoDB connection setup
β βββ routes/ # API route definitions
β βββ controllers/ # Business logic and request handlers
β βββ models/ # Mongoose schemas and models
β βββ middleware/ # Authentication, validation, security, and error handling
β βββ ...
β βββ services/ # Reusable service and domain logic
β βββ utils/ # Shared helper utilities
frontend/
βββ index.html # HTML template for Vite
βββ src/
β βββ main.jsx # React app bootstrap
β βββ App.jsx # Root component, routes, and auth hydration
β βββ store/ # Redux store and slices
β βββ hooks/ # Custom hooks used across the app
β βββ components/ # Reusable UI components
β βββ pages/ # Page-level views and routing targets
β βββ styles/ # Global styles and theme variables
backend/server.jsloads environment variables and starts the Express app.backend/src/config/database.jsconnects to MongoDB usingMONGODB_URI.backend/src/app.jsconfigures middleware:- CORS with origin validation and cookies.
- Compression and JSON parsing.
- Cookie parsing.
- Rate limiting.
- Security protections (Helmet, XSS, HPP, Mongo sanitization).
- API routes are mounted under
/api/v1/. - Protected routes verify JWT tokens using auth middleware.
- Errors are handled globally.
Create a .env file in backend/ with at least:
PORT=5000
MONGODB_URI=mongodb://localhost:27017/ecommerce-db
JWT_ACCESS_SECRET=yourAccessSecret
JWT_REFRESH_SECRET=yourRefreshSecret
JWT_ACCESS_EXPIRES=15m
JWT_REFRESH_EXPIRES=7d
CORS_ORIGIN=http://localhost:5173
FRONTEND_URL=http://localhost:5173
NODE_ENV=development- The React app loads in the browser.
- Routes render the requested page.
- Auth state is restored from backend cookies.
- User actions call backend APIs for auth, products, cart, and orders.
- Protected routes block access for unauthenticated or unauthorized users.
- Redux stores user, cart, and UI state.
cd backend
npm install
cd ../frontend
npm installFrom the repo root:
npm run dev:backendOr from backend/:
npm run devFrom the repo root:
npm run dev:frontendOr from frontend/:
cd frontend
npm run dev- Frontend:
http://localhost:5173 - Backend API base URL:
http://localhost:5000/api/v1 - Health check:
http://localhost:5000/health
POST /api/v1/auth/registerβ Register a new user.POST /api/v1/auth/loginβ Login and receive auth cookies.GET /api/v1/auth/meβ Get current user.PATCH /api/v1/auth/profileβ Update profile.POST /api/v1/auth/logoutβ Logout and clear cookies.POST /api/v1/auth/refreshβ Refresh the access token.
GET /api/v1/productsβ List products.GET /api/v1/products/:idβ Get one product.POST /api/v1/productsβ Create product (admin only).PUT /api/v1/products/:idβ Update product (admin only).DELETE /api/v1/products/:idβ Delete product (admin only).
GET /api/v1/cartβ Get current cart.POST /api/v1/cart/itemsβ Add item.PUT /api/v1/cart/items/:itemIdβ Update quantity.DELETE /api/v1/cart/items/:itemIdβ Remove item.
POST /api/v1/ordersβ Create order.GET /api/v1/ordersβ Get user's orders.GET /api/v1/orders/allβ Get all orders (admin only).GET /api/v1/orders/:idβ Get order details.PATCH /api/v1/orders/:id/statusβ Update order status (admin only).
GET /api/v1/admin/dashboardβ Admin statistics.GET /api/v1/admin/usersβ List users.PATCH /api/v1/admin/users/:userIdβ Update user role/status.DELETE /api/v1/admin/users/:userIdβ Delete a user.
npm run dev:backendβ Start backend dev server.npm run dev:frontendβ Start frontend dev server.npm run build:frontendβ Build frontend.npm run build:backendβ Start backend in production mode.npm run test:backendβ Run backend tests.
npm run devβ Start backend with nodemon.npm startβ Start backend with node.npm testβ Run Jest tests.
npm run devβ Start Vite.npm run buildβ Build production app.npm run previewβ Preview built app.
- JWT access and refresh tokens stored in HTTP-only cookies.
- CORS origin whitelist.
- Rate limiting for API requests.
- Helmet secure headers.
- XSS cleaning, HPP protection, and MongoDB input sanitization.
- Install dependencies in
backend/andfrontend/. - Create
backend/.envwith the required variables. - Start backend and frontend.
- Visit
http://localhost:5173. - Register, login, and explore the storefront and admin pages.
React, Vite, Redux Toolkit, Node.js, Express, MongoDB, JWT, Helmet.