Enterprise-grade TypeScript REST API for an e-commerce platform built with Express.js, MongoDB, and Redis.
- Runtime: Node.js (v18+)
- Language: TypeScript (Strict Mode)
- Framework: Express.js
- Database: MongoDB (Mongoose ODM)
- Cache: Redis
- Documentation: Swagger/OpenAPI
- Testing: Jest + Supertest
- Code Quality: ESLint + Prettier
- CI/CD: GitHub Actions (planned)
- ✅ Clean layered architecture (Routes → Controller → Service → Repository → Model)
- ✅ TypeScript with strict mode and path aliases
- ✅ Base classes for controllers, services, and repositories
- ✅ Comprehensive error handling and validation
- ✅ MongoDB transactions support
- ✅ User authentication (JWT-based)
- ✅ Product management (CRUD operations)
- ✅ Order management with stock tracking
- ✅ Advanced filtering and search
- ✅ Pagination and sorting
- ✅ Soft delete for products
- ✅ Role-based access control (User/Admin)
- ✅ Product search by name (case-insensitive)
- ✅ Price range filtering
- ✅ Category filtering
- ✅ Stock range filtering
- ✅ Status filtering (active/deleted)
- ✅ Order status filtering
- ✅ Date range filtering for orders
- ✅ Docker & Docker Compose setup
- ✅ Production-grade multi-stage Dockerfile
- ✅ MongoDB replica set for transactions
- ✅ Redis caching with automatic invalidation
- ✅ Rate limiting (API and admin endpoints)
- ✅ File upload with Multer (product images)
- ✅ Health checks for all services
- ✅ Non-root container execution
- ✅ Sentry error tracking integration
- ✅ Winston structured logging
- ✅ Swagger/OpenAPI documentation
- ✅ 105+ passing tests (Jest + Supertest)
- ✅ Hot reload in development
- ✅ ESLint + Prettier for code quality
- ✅ CI/CD ready
- ✅ Comprehensive documentation
A2sv/
├── src/
│ ├── app.ts # Express app setup
│ ├── server.ts # Entry point
│ ├── bootstrap/ # Initialization modules
│ │ ├── database.ts # MongoDB connection
│ │ ├── redis.ts # Redis connection
│ │ ├── routes.ts # Route aggregator
│ │ └── dbSeeder.ts # Database seeder
│ ├── config/ # Configuration
│ │ ├── default.json # App config
│ │ └── index.ts # Config loader
│ ├── docs/ # Swagger documentation
│ │ └── swagger.yaml # OpenAPI specification
│ ├── controllers/ # Route handlers
│ │ ├── BaseController.ts # Base controller class
│ │ ├── user.controller.ts # User authentication
│ │ ├── product.controller.ts # Product management
│ │ └── order.controller.ts # Order management
│ ├── services/ # Business logic
│ │ ├── BaseService.ts # Base service class
│ │ ├── user.service.ts # User logic
│ │ ├── product.service.ts # Product logic
│ │ └── order.service.ts # Order logic with transactions
│ ├── repositories/ # Data access layer
│ │ ├── BaseRepository.ts # Base repository class
│ │ ├── user.repository.ts # User data access
│ │ ├── product.repository.ts # Product data access
│ │ └── order.repository.ts # Order data access
│ ├── models/ # Mongoose schemas
│ │ ├── user.model.ts # User schema
│ │ ├── product.model.ts # Product schema
│ │ └── order.model.ts # Order schema
│ ├── routes/ # Route definitions
│ │ ├── auth.routes.ts # Authentication routes
│ │ ├── product.routes.ts # Product routes
│ │ └── order.routes.ts # Order routes
│ ├── middlewares/ # Custom middleware
│ │ ├── auth.middleware.ts # JWT authentication
│ │ ├── cache.middleware.ts # Redis caching
│ │ ├── error.middleware.ts # Error handling
│ │ └── rateLimiter.middleware.ts # Rate limiting
│ ├── validators/ # Request validation
│ │ ├── middleware.ts # Joi validation middleware
│ │ ├── user.validator.ts # User validation schemas
│ │ ├── product.validator.ts # Product validation schemas
│ │ └── order.validator.ts # Order validation schemas
│ ├── helpers/ # Utility functions
│ │ ├── responses/ # Response helpers
│ │ └── multer.helper.ts # File upload configuration
│ ├── utils/ # Utility modules
│ │ └── logger.ts # Winston logger
│ └── types/ # Type definitions
│ └── index.ts # Type exports and interfaces
├── tests/ # Test files (105 tests)
│ ├── auth/ # Authentication tests
│ ├── products/ # Product tests
│ └── orders/ # Order tests
├── uploads/ # File uploads directory
├── Dockerfile # Production Docker image
├── Dockerfile.dev # Development Docker image
├── docker-compose.yml # Docker services
├── docker-compose.dev.yml # Development overrides
├── .dockerignore # Docker ignore rules
├── .env # Environment variables
├── .env.example # Environment template
├── tsconfig.json # TypeScript configuration
├── jest.config.js # Jest configuration
├── .eslintrc.js # ESLint configuration
├── .prettierrc # Prettier configuration
├── DOCKER_GUIDE.md # Docker documentation
└── package.json # Project manifest
- Node.js (v20 or higher)
- MongoDB (running locally or remote)
- Redis (for caching)
- npm or yarn
- Docker & Docker Compose (optional, for containerized setup)
git clone <repository-url>
cd a2sv-ecommerce-backendnpm installCreate a .env file in the root directory (or copy from .env.example):
# Server Configuration
NODE_ENV=development
PORT=5000
# Database Configuration
# With replica set for transactions support
MONGODB_URI=mongodb://localhost:27017/a2sv_ecommerce?replicaSet=rs0&directConnection=true
# Redis Configuration
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_TTL=3600
# JWT Configuration
JWT_SECRET=dev-secret-key-change-in-production
JWT_EXPIRES_IN=7d
# Security
BCRYPT_SALT_ROUNDS=10
# CORS
CORS_ORIGIN=*
# File Upload
MAX_FILE_SIZE=5242880
UPLOAD_DIR=uploads
Why? This API uses MongoDB transactions for atomic operations (order placement, product updates). Transactions require a replica set, even for local development.
Choose ONE setup method:
Run as Administrator in PowerShell:
# 1. Stop local MongoDB if running
Stop-Service MongoDB
# 2. Start MongoDB with replica set using Docker
docker-compose up -d mongo mongo-init
# 3. Wait for initialization
Start-Sleep -Seconds 15
# 4. Verify replica set
docker exec -it a2sv-mongodb mongosh --eval "rs.status()"You should see "stateStr": "PRIMARY" in the output.
# Run as Administrator
.\setup-mongodb-replicaset.ps1
# Verify
mongosh --eval "rs.status()"- Edit MongoDB config:
C:\Program Files\MongoDB\Server\7.0\bin\mongod.cfg - Add at the end:
replication: replSetName: "rs0"
- Restart MongoDB:
net stop MongoDB net start MongoDB
- Initialize replica set:
mongosh --eval "rs.initiate({_id: 'rs0', members: [{_id: 0, host: 'localhost:27017'}]})"
✅ Your .env is already configured with:
MONGODB_URI=mongodb://localhost:27017/a2sv_ecommerce?replicaSet=rs0&directConnection=trueRedis (optional):
# Using Redis locally
redis-server
# Or using Docker (if not using docker-compose)
docker run -d -p 6379:6379 --name redis redis:alpineDevelopment mode (with auto-reload):
npm run devProduction mode:
npm run build
npm startStart all services with Docker:
# Production mode
docker compose up -d
# Development mode with hot reload
docker compose -f docker-compose.yml -f docker-compose.dev.yml up
# View logs
docker compose logs -f
# Stop all services
docker compose down# Run all tests (105 tests)
npm test
# Run tests with coverage
npm run test:coverage
# Run tests in watch mode
npm run test:watch
# Run specific test suite
npm test -- --testPathPattern=products
npm test -- --testPathPattern=orders
npm test -- --testPathPattern=authTest Coverage:
- ✅ Authentication (Register, Login)
- ✅ Products (CRUD, Search, Filtering)
- ✅ Orders (Place Order, Order History, Order Details)
- ✅ Pagination & Sorting
- ✅ Error Handling
- ✅ Security (JWT, Role-based access)
Once the server is running, visit:
- Swagger UI: http://localhost:5000/api-docs
- Health Check: http://localhost:5000/health
POST /api/v1/auth/register- Register new userPOST /api/v1/auth/login- Login user
GET /api/v1/products- List products (with filtering)GET /api/v1/products/:id- Get product details
POST /api/v1/products- Create product (with optional image upload)PUT /api/v1/products/:id- Update product (with optional image upload)DELETE /api/v1/products/:id- Delete product (soft delete)
POST /api/v1/orders- Place new orderGET /api/v1/orders- Get order history (with filtering)GET /api/v1/orders/:id- Get order details
PATCH /api/v1/orders/:id/status- Update order status
Product Filters:
# Search by name
GET /api/v1/products?search=laptop
# Price range
GET /api/v1/products?minPrice=100&maxPrice=500
# Category
GET /api/v1/products?category=electronics
# Stock range
GET /api/v1/products?minStock=10&maxStock=100
# In stock only
GET /api/v1/products?inStock=true
# Combined filters
GET /api/v1/products?category=electronics&maxPrice=500&inStock=true&page=1&limit=20Order Filters:
# By status
GET /api/v1/orders?status=pending
# Price range
GET /api/v1/orders?minPrice=100&maxPrice=500
# Date range
GET /api/v1/orders?startDate=2024-01-01&endDate=2024-12-31
# Combined
GET /api/v1/orders?status=delivered&minPrice=500&sort=-totalPriceThe API supports optional image uploads for products using Multer with local disk storage.
Features:
- ✅ Optional single image per product
- ✅ Supported formats: JPEG, PNG, GIF, WebP, PDF
- ✅ Maximum file size: 5MB
- ✅ UUID-based filenames for uniqueness
- ✅ Images stored in
uploads/products/directory - ✅ Automatic path transformation to API-accessible URLs
- ✅ Static file serving at
/api/uploads/products/
Creating a product with image (cURL):
curl -X POST http://localhost:5000/api/v1/products \
-H "Authorization: Bearer YOUR_TOKEN" \
-F "name=Wireless Headphones" \
-F "description=High-quality wireless headphones" \
-F "price=99.99" \
-F "stock=50" \
-F "category=Electronics" \
-F "productImage=@/path/to/image.jpg"Response includes image URL:
{
"success": true,
"message": "Product created successfully",
"data": {
"_id": "507f1f77bcf86cd799439011",
"name": "Wireless Headphones",
"productImage": "/api/uploads/products/abc123-uuid.jpg",
...
}
}Accessing uploaded images:
# Direct browser access
http://localhost:5000/api/uploads/products/abc123-uuid.jpg
# Or via cURL
curl http://localhost:5000/api/uploads/products/abc123-uuid.jpg --output image.jpgImplementation Details:
- Multer Helper:
src/helpers/multer.helper.tshandles file upload configuration - Storage: Files saved to
uploads/products/with UUID filenames - Path Transformation: Automatically converts local paths to API URLs
- Validation: File type and size validation at middleware level
- Static Serving: Configured in
src/app.tsfor public access
| Script | Description |
|---|---|
npm run dev |
Start development server with auto-reload |
npm run build |
Build TypeScript to JavaScript |
npm start |
Start production server |
npm test |
Run Jest tests with coverage |
npm run test:watch |
Run tests in watch mode |
npm run lint |
Run ESLint |
npm run lint:fix |
Fix ESLint errors |
npm run format |
Format code with Prettier |
npm run format:check |
Check code formatting |
| Command | Description |
|---|---|
docker compose up -d |
Start all services (backend, mongo, redis) |
docker compose down |
Stop all services |
docker compose logs -f |
View service logs |
docker compose ps |
Check service status |
docker compose build |
Rebuild images |
docker compose -f docker-compose.yml -f docker-compose.dev.yml up |
Start development environment |
- ✅ Clean folder structure
- ✅ TypeScript configuration with strict mode
- ✅ Express app with security middleware
- ✅ MongoDB and Redis connection setup
- ✅ Swagger documentation
- ✅ Code quality tools (ESLint, Prettier)
- ✅ Jest testing configuration
- ✅ Environment configuration
- ✅ User authentication (Register, Login)
- ✅ Product CRUD operations
- ✅ Order management with transactions
- ✅ JWT authentication middleware
- ✅ Request validation middleware
- ✅ Role-based access control
- ✅ 105+ integration tests
- ✅ Soft delete functionality
- ✅ Advanced filtering (6 product filters, 3 order filters)
- ✅ Pagination and sorting
- ✅ Search functionality
- ✅ MongoDB transactions for orders
- ✅ Stock management
- ✅ Base classes for reusability
- ✅ Comprehensive error handling
- ✅ Redis caching with automatic invalidation
- ✅ Rate limiting (100 req/15min API, 20 req/5min admin)
- ✅ File upload for product images (Multer + local storage)
- ✅ API versioning (v1)
- ✅ Order status update endpoint (admin)
- ✅ Sentry error tracking
- ✅ Winston structured logging
- ✅ Docker setup (multi-stage builds)
- ✅ Docker Compose configuration
- ✅ Development and production environments
- ✅ Health checks
- ✅ Non-root container execution
- ✅ MongoDB replica set for transactions
- ✅ Resource limits and security
- Cloudinary integration for image hosting
- Multiple images per product
- Image optimization and thumbnails
- Email notifications
- Payment integration
- Admin dashboard
- Inventory management
- Reporting and analytics
- WebSocket for real-time updates
| Variable | Description | Default |
| NODE_ENV | Environment (development/production) | development |
| PORT | Server port | 5000 |
| MONGODB_URI | MongoDB connection string | mongodb://localhost:27017/a2sv_ecommerce |
| REDIS_HOST | Redis host | localhost |
| REDIS_PORT | Redis port | 6379 |
| JWT_SECRET | Secret key for JWT | required |
| JWT_EXPIRES_IN | JWT expiration time | 7d |
| BCRYPT_SALT_ROUNDS | Bcrypt salt rounds | 12 |
| CORS_ORIGIN | CORS allowed origins | * |
- Docker Guide - Complete Docker setup and deployment guide
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'feat: add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- ✅ All tests must pass (105+ tests)
- ✅ TypeScript strict mode compliance
- ✅ ESLint with no errors
- ✅ Prettier formatting
- ✅ Follow layered architecture pattern
- ✅ Add tests for new features
This project is licensed under the MIT License.
A2SV Backend Developer
# Clone repository
git clone <repository-url>
cd A2sv
# Install dependencies
npm install
# Setup environment
cp .env.example .env
# Edit .env with your configuration
# Start with Docker (recommended)
docker compose up -d
# Or start locally
npm run dev
# Run tests
npm test
# Access API
curl http://localhost:5000/health
# View API documentation
open http://localhost:5000/api-docs🚀 Production-ready enterprise e-commerce API with Docker support!