A comprehensive e-commerce API built with Node.js, Express, TypeScript, and MongoDB, featuring JWT authentication, role-based authorization, and a clean architecture pattern.
This project includes comprehensive documentation covering all aspects of the system:
-
Project Structure & Architecture
- Clean Architecture implementation
- Domain-Driven Design patterns
- Dependency injection setup
- File organization and naming conventions
-
Authentication & Authorization
- JWT-based authentication flow
- Refresh token rotation
- Role-based access control (RBAC)
- Security mechanisms and best practices
-
- Custom error classes hierarchy
- Global error handler implementation
- Consistent error response formats
- Localized error messages (Persian)
-
- Authentication and authorization middleware
- Security middleware (XSS, CORS, Rate limiting)
- Validation middleware integration
- Domain-specific middleware patterns
-
- Swagger/OpenAPI 3.0 specification
- Interactive API documentation
- Request/response schemas
- Authentication examples
-
- Integration testing approach
- Jest and Supertest configuration
- Test helpers and utilities
- Database testing with MongoDB Memory Server
- Node.js (v18 or higher)
- MongoDB
- npm or yarn
# Clone the repository
git clone <repository-url>
cd shop-flow
# Install dependencies
npm install
# Set up environment variables
cp .env.example .env
# Edit .env with your configuration
# Start development server
npm run start:dev# Database
MONGO_URI=mongodb://localhost:27017/shop-flow
# JWT Configuration
JWT_SECRET=your_access_token_secret
JWT_EXPIRES_IN=15m
JWT_REFRESH_SECRET=your_refresh_token_secret
JWT_REFRESH_EXPIRES_IN=7d
# Email Configuration
EMAIL_FROM=noreply@shopflow.com
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_USERNAME=your_email@gmail.com
EMAIL_PASSWORD=your_app_password
# Cloudinary (for image uploads)
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret
# Application
NODE_ENV=development
PORT=3000Shop Flow follows a Clean Architecture pattern with Domain-Driven Design principles:
src/
βββ app/ # Application layer (configuration, routes)
βββ core/ # Domain layer (business logic)
β βββ users/ # User domain
β βββ products/ # Product domain
β βββ orders/ # Order domain
β βββ reviews/ # Review domain
β βββ uploads/ # Upload domain
βββ middlewares/ # Infrastructure layer (cross-cutting concerns)
βββ errors/ # Custom error classes
βββ utils/ # Utility functions
βββ __tests__/ # Test suite
- JWT Authentication with refresh token rotation
- Role-based Authorization (User, Admin)
- Input Validation with express-validator
- Error Handling with custom error classes
- File Upload with Cloudinary integration
- API Documentation with Swagger UI
- Testing with Jest and Supertest
- Security with Helmet, CORS, Rate limiting
- Database with Mongoose ODM
POST /api/users/signup- User registrationPOST /api/users/login- User loginPOST /api/users/logout- User logoutPOST /api/users/forgot-password- Password reset requestPATCH /api/users/reset-password- Password resetPOST /api/users/refresh-token- Token refresh
GET /api/users/get-me- Get current user profilePATCH /api/users/update-me- Update user profilePATCH /api/users/update-me-password- Change passwordDELETE /api/users/delete-me- Delete user account
GET /api/products- Get all products (with filtering, sorting, pagination)GET /api/products/:id- Get product by IDPOST /api/products- Create product (Admin only)PATCH /api/products/:id- Update product (Admin only)DELETE /api/products/:id- Delete product (Admin only)
GET /api/orders/get-myorders- Get current user's ordersGET /api/orders/:id- Get order by IDPOST /api/orders- Create new orderPATCH /api/orders/:id/pay- Mark order as paidGET /api/orders- Get all orders (Admin only)PATCH /api/orders/:id- Update order (Admin only)DELETE /api/orders/:id- Delete order (Admin only)PATCH /api/orders/:id/deliver- Mark order as delivered (Admin only)
GET /api/products/:productId/reviews- Get product reviewsPOST /api/products/:productId/reviews- Create reviewGET /api/products/:productId/reviews/:id- Get review by IDPATCH /api/products/:productId/reviews/:id- Update reviewDELETE /api/products/:productId/reviews/:id- Delete review
POST /api/uploads- Upload image file
GET /api/users- Get all users (Admin only)POST /api/users- Create user (Admin only)GET /api/users/:id- Get user by ID (Admin only)PATCH /api/users/:id- Update user (Admin only)DELETE /api/users/:id- Delete user (Admin only)GET /api/users/get-users-count- Get user statistics (Admin only)
- User Registration/Login: User submits credentials
- Token Generation: JWT access token (15 min) + refresh token (7 days)
- Token Storage: Tokens stored in httpOnly cookies + headers
- Request Authentication: Middleware validates tokens on protected routes
- Token Refresh: Automatic token refresh when access token expires
- Logout: Tokens cleared from cookies and database
- JWT Authentication with separate access/refresh tokens
- Password Hashing with bcrypt
- Input Sanitization against XSS and NoSQL injection
- Rate Limiting to prevent abuse
- CORS Configuration for cross-origin requests
- Helmet for security headers
- HTTP Parameter Pollution protection
The project uses Jest with Supertest for integration testing:
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:coverage- Integration Tests: End-to-end API testing
- Test Helpers: Reusable test utilities
- In-Memory Database: MongoDB Memory Server for isolation
- Mock Services: External service mocking
Interactive API documentation is available at:
- Development:
http://localhost:3000/api-docs - Production:
https://your-domain.com/api-docs
The documentation includes:
- Complete endpoint specifications
- Request/response schemas
- Authentication examples
- Interactive testing interface
Here are the frontend projects currently using this API:
- π Visit Azoghe
- π Azoghe Repository
To help frontend developers set up their initial database data, a CMS has been developed:
# Build the application
npm run build
# Start production server
npm startEnsure all environment variables are properly configured for production:
- Use strong JWT secrets
- Configure proper CORS origins
- Set up email service credentials
- Configure Cloudinary for file uploads
- Use production MongoDB instance
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow the existing code structure and patterns
- Write tests for new features
- Update documentation as needed
- Follow TypeScript best practices
- Use meaningful commit messages
This project is licensed under the ISC License - see the LICENSE file for details.
Hossein Rezaei
- Email: rezaeig22@gmail.com
- Telegram: @h3nrzi
For detailed information about any aspect of the system, please refer to the specific documentation files listed above. Each document provides comprehensive coverage of its respective topic with examples, best practices, and implementation details.







