RESTful API backend for Emobo e-commerce platform built with Express.js, Prisma ORM, PostgreSQL, and Docker.
- Features
- Tech Stack
- Architecture
- Getting Started
- Environment Variables
- API Documentation
- Database Schema
- Project Structure
- Development
- β JWT-based authentication (access + refresh tokens)
- β Role-based access control (Admin, Customer)
- β Secure password hashing with bcrypt
- β Token refresh mechanism
- β Logout with token revocation
- β CRUD operations for products
- β Product search and filtering
- β Stock management
- β Multiple product images support
- β SKU-based product identification
- β Create orders with multiple items
- β Order status tracking (PENDING β PROCESSING β SHIPPED β COMPLETED)
- β Order history for customers
- β Admin order management
- β Tracking number support
- β Multiple payment gateway support (Mock, Duitku, Flip)
- β QRIS payment code generation
- β Payment webhook handling
- β Payment status tracking
- β Automatic order status updates
- β User profile management
- β Order history viewing
- β Order detail tracking
- β Product management (create, update, delete)
- β Order management and status updates
- β Customer listing
- β Payment monitoring
| Category | Technology |
|---|---|
| Runtime | Node.js 22+ |
| Language | TypeScript |
| Framework | Express.js |
| ORM | Prisma |
| Database | PostgreSQL |
| Container | Docker |
| Authentication | JWT (jsonwebtoken) |
| Validation | Zod |
| Password Hashing | bcrypt |
| Payment | Duitku, Flip (optional) |
βββββββββββββββ
β Client β
ββββββββ¬βββββββ
β
βΌ
βββββββββββββββββββββββββββββββ
β Express Router β
βββββββββββββββββββββββββββββββ€
β βββββββββββββββββββββββ β
β β Auth Middleware β β
β βββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββ€
β Controllers β
β ββββββββ¬βββββββ¬βββββββββ β
β β Auth β Prod β Order β β
β ββββββββ΄βββββββ΄βββββββββ β
βββββββββββββββββββββββββββββββ€
β Services β
β ββββββββ¬βββββββ¬βββββββββ β
β β Auth β Prod β Paymentβ β
β ββββββββ΄βββββββ΄βββββββββ β
βββββββββββββββββββββββββββββββ€
β Prisma Client β
ββββββββββββ¬βββββββββββββββββββ
β
βΌ
ββββββββββββββββ
β PostgreSQL β
ββββββββββββββββ
- Node.js (v18 or higher)
- PostgreSQL (v14 or higher)
- npm or pnpm
- Clone the repository
git clone <repository-url>
cd backend- Install dependencies
npm install- Set up environment variables
Create .env file:
DATABASE_URL="postgresql://user:password@localhost:5432/emobo_db"
JWT_ACCESS_SECRET="your-access-secret"
JWT_REFRESH_SECRET="your-refresh-secret"
PORT=5000
NODE_ENV=development- Set up database
# Create database
createdb emobo_db
# Run migrations
npx prisma migrate dev --name init
# Generate Prisma Client
npx prisma generate- Start development server
npm run devServer will start at http://localhost:5000
- Build the image
docker build -t emobo-api .- Run the container
docker run -p 5000:5000 --env-file .env emobo-apiTip
Use host.docker.internal for DATABASE_URL if your PostgreSQL is running on the host machine (Windows/macOS).
| Variable | Description | Example |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | postgresql://user:pass@localhost:5432/db |
JWT_ACCESS_SECRET |
Secret key for access tokens | Random string (32+ chars) |
JWT_REFRESH_SECRET |
Secret key for refresh tokens | Random string (32+ chars) |
PORT |
Server port | 5000 |
NODE_ENV |
Environment mode | development / production |
DUITKU_MERCHANT_CODE |
Duitku merchant code (optional) | DS12345 |
DUITKU_API_KEY |
Duitku API key (optional) | your-api-key |
DUITKU_CALLBACK_URL |
Payment webhook URL | https://api.example.com/payments/callback |
http://localhost:5000/api/v1
POST /auth/register - Register new user
POST /auth/login - Login user
POST /auth/refresh - Refresh access token
POST /auth/logout - Logout user
GET /products - Get all products (with filters)
GET /products/:id - Get product by ID
POST /products - Create product (Admin only)
PUT /products/:id - Update product (Admin only)
DELETE /products/:id - Delete product (Admin only)
POST /orders - Create new order
GET /orders - Get all orders (Admin only)
GET /orders/:id - Get order by ID
PATCH /orders/:id/status - Update order status (Admin only)
POST /payments - Create payment
POST /payments/callback - Payment webhook callback
GET /payments/:id - Get payment status
GET /customers/profile - Get customer profile
PUT /customers/profile - Update customer profile
GET /customers/orders - Get customer orders
Most endpoints require JWT authentication. Include access token in cookie or Authorization header:
# Cookie-based (automatic)
Cookie: accessToken=your-jwt-token
# Header-based
Authorization: Bearer your-jwt-tokenUser
- Manages user accounts (customers and admins)
- Stores hashed passwords
- Linked to orders and refresh tokens
Product
- Product catalog with SKU, pricing, stock
- Supports multiple images
- Linked to order items
Order
- Customer orders with status tracking
- Contains shipping address and phone
- Linked to order items and payments
OrderItem
- Individual items within an order
- Stores quantity and unit price snapshot
Payment
- Payment transactions with provider integration
- QRIS code storage
- Webhook-based status updates
RefreshToken
- Manages JWT refresh tokens
- Supports token revocation
See ERD Documentation for detailed schema.
backend/
βββ prisma/
β βββ migrations/ # Database migrations
β βββ schema.prisma # Database schema
βββ src/
β βββ middleware/ # Auth, error handling
β βββ modules/
β β βββ auth/ # Auth controller, service, routes
β β βββ products/ # Product CRUD
β β βββ orders/ # Order management
β β βββ payments/ # Payment processing
β β βββ customers/ # Customer features
β βββ routes/ # API routes
β βββ utils/ # Helper functions
β βββ app.ts # Express app setup
β βββ server.ts # Server entry point
βββ .env # Environment variables
βββ package.json
βββ tsconfig.json
# Development
npm run dev # Start dev server with hot-reload
# Production
npm run build # Build TypeScript
npm start # Start production server
# Database
npx prisma studio # Open database GUI
npx prisma migrate dev # Create new migration
npx prisma generate # Regenerate Prisma Client
# Utilities
npm run format # Format code
npm run lint # Lint code- Create module structure
src/modules/feature/
βββ feature.controller.ts
βββ feature.service.ts
βββ feature.route.ts- Define controller - Handle HTTP requests
- Define service - Business logic
- Define routes - API endpoints
- Register routes in
app.ts - Update Prisma schema if needed
- Create migration -
npx prisma migrate dev
# Create migration
npx prisma migrate dev --name feature_name
# Apply migrations
npx prisma migrate deploy
# Reset database (dev only)
npx prisma migrate resetUse Prisma Studio to manage test data:
npx prisma studioTest endpoints with curl:
# Health check
curl http://localhost:5000/api/v1/health
# Register user
curl -X POST http://localhost:5000/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"password123","name":"Test User"}'Prisma Client not generated
npx prisma generateDatabase connection error
- Check PostgreSQL is running
- Verify DATABASE_URL in .env
- Ensure database exists
Migration errors
npx prisma migrate reset # Reset and reapply all migrationsPort already in use
npx kill-port 5000 # Kill process on port 5000- ERD - Database entity relationships
- Class Diagram - System architecture
- Sequence Diagrams - Request flows
- How to Run - Complete setup guide
- Passwords hashed with bcrypt (salt rounds: 10)
- JWT tokens with expiration
- Role-based access control
- SQL injection prevention via Prisma
- Input validation with Zod
- CORS configuration
- Refresh token rotation
This project uses GitHub Actions for continuous integration and delivery.
- Build & Push: Triggered on push to
mainbranch. - Workflow:
.github/workflows/main.yml - Docker Hub: Automatically pushes image to
daffa09/emobo-ecommerce-api:latest.
- Set strong JWT secrets
- Use production database
- Enable HTTPS
- Configure CORS properly
- Set up environment variables
- Run migrations
- Configure payment webhooks
- Set up monitoring
- Configure rate limiting
- Set up backups
For a complete setup with UI and Gateway, use the docker-compose.yml located in the deployments/ folder:
cd deployments
docker compose up -d- Railway - Easy deployment with database
- Heroku - Classic PaaS
- DigitalOcean - VPS deployment
- AWS/GCP - Enterprise solutions
This project is part of a thesis/skripsi project.
Built as part of academic project at [Your University]
For detailed setup instructions, see HOW_TO_RUN.md