# 🛒 E-Commerce Backend API (Node.js + Express + TypeScript + Prisma)
A scalable and modular e-commerce backend API built with modern tools and best practices.
## 📦 Tech Stack
- **Runtime**: Node.js + Express
- **Language**: TypeScript
- **ORM**: Prisma
- **Database**: PostgreSQL (or MySQL)
- **Authentication**: JWT (access & refresh tokens)
- **Validation**: Zod
- **Structure**: Modular (MVC-style)
- **Security**: Auth middleware, role-based access
---
## 🧱 Project Structure
src/ ├── config/ # Environment config ├── lib/ # Prisma client, utility functions ├── middleware/ # Auth and error middleware ├── modules/ │ ├── auth/ # Auth logic (signup, login, refresh) │ ├── product/ # Product & category CRUD │ ├── cart/ # Cart operations │ ├── order/ # Checkout & order history ├── routes/ # Route registration └── server.ts # Entry point
---
## 🔐 Auth Features
- JWT Access + Refresh Tokens
- `isAuthenticated` middleware
- User model with roles
### Routes
| Method | Endpoint | Description |
|--------|------------------|---------------------|
| POST | `/auth/register` | Register new user |
| POST | `/auth/login` | Login and get token |
| POST | `/auth/refresh` | Refresh JWT token |
| GET | `/auth/me` | Get current user |
---
## 🛍️ Product Module
- Product + Category models
- Auto-slug generation
- CRUD for admin
- Public browse endpoints
| Method | Endpoint | Description |
|--------|----------------------|---------------------|
| GET | `/products` | List all products |
| POST | `/products` | Create product |
| GET | `/products/:id` | Get product details |
| PATCH | `/products/:id` | Update product |
| DELETE | `/products/:id` | Delete product |
---
## 🛒 Cart Module
- Add/remove/update items
- One cart per user
- Quantity updates
- Automatically linked to products
| Method | Endpoint | Description |
|--------|------------------|--------------------|
| GET | `/cart` | Get current cart |
| POST | `/cart` | Add item to cart |
| PATCH | `/cart/:itemId` | Update quantity |
| DELETE | `/cart/:itemId` | Remove cart item |
---
## 📦 Order Module
- Place order from cart
- Store price snapshot
- View past orders
- Order status enum
### Order Status
```prisma
enum OrderStatus {
PENDING
PAID
CANCELLED
SHIPPED
}
| Method | Endpoint | Description |
|---|---|---|
| POST | /orders/checkout |
Checkout cart |
| GET | /orders |
List user orders |
| GET | /orders/:id |
Get order by ID |
Each module validates its input using zod schemas, e.g.:
export const authSchema = z.object({
email: z.string().email(),
password: z.string().min(6),
});// req.user is injected after verifying JWT
req.user = { id, email, role };Use in routes:
router.use(isAuthenticated);model User {
id String @id @default(uuid())
email String @unique
password String
role Role @default(CUSTOMER)
...
}model Product {
id String @id @default(uuid())
name String
description String
slug String @unique
price Float
stock Int
categoryId String
...
}model CartItem {
id String @id @default(uuid())
userId String
productId String
quantity Int @default(1)
}model Order {
id String @id @default(uuid())
userId String
total Float
status OrderStatus @default(PENDING)
...
}npm install
npx prisma generate
npx prisma migrate dev
npm run dev- ✅ Stripe integration (Payments)
- ✅ Admin dashboard endpoints
- ✅ Email confirmation (resend, verify)
- ✅ Pagination & filters
- ✅ Reviews & ratings
Pull requests and forks are welcome. Please open an issue for any feature request or bug.
MIT – free to use and modify.