- MERN E-Commerce ποΈβ¨
MERN E-Commerce is a full-stack TypeScript application implementing a modern online shop: user auth (access + refresh tokens with Redis), cart management, Stripe Checkout, coupon generation/validation, Cloudinary image uploads, Redis caching for featured products, admin product management and analytics.
Backend is an Express + TypeScript app (/backend) and frontend is React + Vite + TypeScript (/frontend).
- Auth & Sessions: Signup/login, httpOnly access & refresh tokens, refresh tokens stored in Redis.
- Product management: Admin CRUD for products, Cloudinary image upload, toggle featured.
- Cart: add/remove items, update quantity, fetch cart items populated with product info.
- Stripe Checkout: create checkout sessions, success handling creates orders and deactivates coupons.
- Coupons: per-user coupon, validation, expiration handling and Stripe coupon sync.
- Caching & Performance: Redis cache for featured products and token store.
- Analytics: totals (users, products, sales, revenue) and daily sales aggregation.
- Security: Helmet CSP configured for Stripe & Cloudinary, CORS, cookie flags, JSON limits.
- Frontend UX: Zustand stores, axios with refresh interceptor, protected routes, responsive UI.
- Node.js β JavaScript runtime for building fast and scalable server-side applications.
- Express β Minimal and flexible Node.js web application framework.
- TypeScript β Strongly typed superset of JavaScript that enhances code quality and developer experience.
- Mongoose β Elegant MongoDB object modeling for Node.js.
- MongoDB β NoSQL database used for storing products, users, orders, and more.
- ioredis β Redis client for Node.js, used for caching and session/token storage.
- Stripe β Payment platform used to handle checkout sessions and transactions.
- Cloudinary β Cloud-based image hosting and transformation service, used for storing and optimizing product images.
- React β Component-based library for building interactive UIs.
- Vite β Fast development build tool for modern web apps.
- TypeScript β Strong typing for better developer tooling and fewer bugs.
- Tailwind CSS β Utility-first CSS framework for rapidly building custom designs.
- Zustand β Lightweight, minimal state-management solution for React.
- Axios β Promise-based HTTP client for making API requests from the browser.
- React Router β Declarative routing for React applications.
- React Hot Toast β Lightweight, customizable toast notifications for user feedback.
- Recharts β Charting library for building customizable data visualizations.
- JWT β JSON Web Tokens used for access and refresh token-based authentication.
- httpOnly Cookies β Used for securely storing refresh tokens in the browser, inaccessible to JavaScript.
- Helmet β Secures Express apps by setting various HTTP headers.
- CORS β Mechanism to allow controlled access to resources from different domains.
- bcrypt β Library for hashing passwords and comparing password hashes securely.
- tsx β Instant TypeScript execution for Node.js using esbuild.
- Vite β Lightning-fast dev server and build tool (also listed in frontend).
- ESLint β Pluggable linting utility for identifying and fixing problems in code.
- TypeScript β Repeated for emphasis; used both client and server side for type safety.
- Secure session flow: short-lived access tokens + refresh tokens stored in Redis.
- Server-authoritative payments: Stripe sessions β server verifies and persists orders.
- Cache-first reads: Redis improves performance for featured products.
- Clean separation: controllers for logic, routes for endpoints, models for schema.
- Robust frontend flow: global token refresh + optimistic UX with Zustand.
- Node.js (v18+ recommended) & npm
- MongoDB (Atlas or local)
- Redis (Upstash or local)
- Cloudinary account (optional for images)
- Stripe account (test keys ok)
-
Clone repository:
git clone https://github.com/surjeetkumar8006/MERN_Ecommerce cd MERN_Ecommerce -
Install dependencies:
npm install npm install --prefix frontend
-
Create .env (see next section).
-
Start backend (with tsx watch):
npm run dev
-
Start frontend (in another terminal):
cd frontend npm run devFrontend default: http://localhost:5173
Backend default: http://localhost:5000 (or the
PORTfrom.env)
From project root:
npm run build
npm run startnpm run buildinstalls frontend deps, builds frontend, compiles backend TS intobackend/dist.npm run startrunsnode backend/dist/server.jsand β whenNODE_ENV !== "development"β serves the frontend build fromfrontend/dist.
Create a .env in the project root:
PORT=5000
NODE_ENV=development
MONGO_URI=mongodb+srv://<user>:<pass>@cluster0.mongodb.net/dbname
UPSTASH_REDIS_URL=redis://<...>
ACCESS_TOKEN_SECRET=your_access_token_secret
REFRESH_TOKEN_SECRET=your_refresh_token_secret
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret
STRIPE_SECRET_KEY=sk_test_...
CLIENT_URL=http://localhost:5173Frontend (client-only) .env:
# frontend/.env
VITE_STRIPE_PUBLISHABLE_KEY=pk_test_...All routes are mounted under
/api/.... Protected routes require theprotectedRoutemiddleware (valid access token cookie). Admin-only routes also requireadminRoute.
-
Auth
POST /signupβ Register new user and set auth cookies.POST /loginβ Log in user and return user data with cookies.POST /logoutβ Logout user and clear cookies. (Protected)POST /refresh-tokenβ Refresh access token using cookie.GET /profileβ Get current user profile. (Protected)
-
Products
GET /productsβ Get all products. (Protected, Admin only)GET /products/featuredβ Get featured products (cached).GET /products/category/:categoryβ Get products by category.GET /products/recommendationsβ Get recommended products.POST /productsβ Create a new product. (Protected, Admin only)PATCH /products/:idβ Toggle featured status. (Protected, Admin only)DELETE /products/:idβ Delete product and image. (Protected, Admin only)
-
Cart
GET /cartβ Get current user's cart. (Protected)POST /cartβ Add or increment product in cart. (Protected)DELETE /cartβ Remove product or clear cart. (Protected)PUT /cart/:idβ Update item quantity. (Protected)
-
Coupons
GET /couponsβ Get active coupon. (Protected)POST /coupons/validateβ Validate coupon code. (Protected)
-
Payments
POST /payments/create-checkout-sessionβ Start Stripe checkout. (Protected)POST /payments/checkout-successβ Confirm payment and create order. (Protected)
Contributions are welcome!
- Fork the repo
- Create branch:
git checkout -b feat/your-feature - Commit:
git commit -m "feat: your feature" - Push & open a PR
Please test major flows (signup/login, checkout, admin product actions) before submitting PRs.

