A comprehensive, production-ready ecommerce backend built with Node.js, Express, and MongoDB. Features include authentication, product management, shopping cart, order processing, and more.
- Authentication & Authorization: JWT-based auth with role-based access control
- Product Management: CRUD operations with search, filtering, and pagination
- Shopping Cart: Persistent cart with session management
- Order Management: Complete order lifecycle from creation to fulfillment
- User Reviews: Product review and rating system
- File Upload: Image upload with Cloudinary integration
- Payment Integration: Stripe payment processing
- Clustering: Multi-core CPU utilization
- Rate Limiting: API request throttling
- Security: Helmet, XSS protection, NoSQL injection prevention
- Compression: Gzip compression for responses
- Caching: Redis integration for session and data caching
- Logging: Structured logging with daily rotation
- Health Checks: Application monitoring endpoints
- Environment Configuration: Comprehensive env variable setup
- Database Seeding: Sample data for development
- Error Handling: Centralized error management
- Process Management: PM2 ready for production
- Node.js 16+
- MongoDB 4.4+ (local installation or MongoDB Atlas)
- Redis (optional, for caching)
-
Clone the repository
git clone <repository-url> cd Heritage spparow/server
-
Install dependencies
npm install
-
Environment Configuration
cp .env.example .env # Edit .env with your configuration -
Start MongoDB (if not using Docker)
# Using MongoDB locally or MongoDB Atlas # Update MONGO_URI in .env file
-
Seed the database (optional)
npm run seed
-
Start development server
npm run dev
-
Install PM2 (Process Manager)
npm install -g pm2
-
Start production server
NODE_ENV=production pm2 start server.js --name Heritage spparow-backend
-
Monitor processes
pm2 status pm2 logs Heritage spparow-backend
Create a .env file with the following variables:
# Server Configuration
PORT=3000
NODE_ENV=development
# Database Configuration
MONGO_URI=mongodb://localhost:27017/Heritage spparow
REDIS_URL=redis://localhost:6379
# JWT Configuration
JWT_SECRET=your_super_secure_jwt_secret_key
JWT_EXPIRE=7d
# CORS Configuration
CLIENT_URL=http://localhost:5173
ALLOWED_ORIGINS=http://localhost:5173,http://localhost:3000
# Email Configuration (Optional)
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_USER=your-email@gmail.com
EMAIL_PASSWORD=your-app-password
# Payment Configuration (Optional)
STRIPE_SECRET_KEY=sk_test_your_stripe_secret_key
STRIPE_PUBLISHABLE_KEY=pk_test_your_stripe_publishable_key
# File Upload Configuration (Optional)
CLOUDINARY_CLOUD_NAME=your-cloud-name
CLOUDINARY_API_KEY=your-api-key
CLOUDINARY_API_SECRET=your-api-secretDevelopment: http://localhost:3000/api
Production: https://your-domain.com/api
| Method | Endpoint | Description | Access |
|---|---|---|---|
| POST | /auth/register |
Register new user | Public |
| POST | /auth/login |
User login | Public |
| GET | /auth/me |
Get current user | Private |
| PUT | /auth/profile |
Update user profile | Private |
| PUT | /auth/password |
Change password | Private |
| Method | Endpoint | Description | Access |
|---|---|---|---|
| GET | /products |
Get all products | Public |
| GET | /products/:id |
Get single product | Public |
| POST | /products |
Create product | Admin |
| PUT | /products/:id |
Update product | Admin |
| DELETE | /products/:id |
Delete product | Admin |
| POST | /products/:id/reviews |
Add review | Private |
| GET | /products/featured |
Get featured products | Public |
| GET | /products/top/rated |
Get top rated products | Public |
| GET | /products/categories |
Get all categories | Public |
| Method | Endpoint | Description | Access |
|---|---|---|---|
| GET | /cart |
Get user cart | Private |
| POST | /cart/add |
Add item to cart | Private |
| PUT | /cart/item/:itemId |
Update cart item | Private |
| DELETE | /cart/item/:itemId |
Remove cart item | Private |
| DELETE | /cart/clear |
Clear entire cart | Private |
| GET | /cart/count |
Get cart item count | Private |
| Method | Endpoint | Description | Access |
|---|---|---|---|
| POST | /orders |
Create new order | Private |
| GET | /orders/my |
Get user orders | Private |
| GET | /orders/:id |
Get single order | Private |
| PUT | /orders/:id/pay |
Update order to paid | Private |
| PUT | /orders/:id/cancel |
Cancel order | Private |
| GET | /orders |
Get all orders | Admin |
| PUT | /orders/:id/status |
Update order status | Admin |
page: Page number (default: 1)limit: Items per page (default: 12)category: Filter by categorybrand: Filter by brandminPrice&maxPrice: Price range filterrating: Minimum rating filtersearch: Search in name, description, tagssortBy: Sort options (price-asc, price-desc, rating, newest, oldest)featured: Filter featured productsinStock: Filter in-stock products only
# Get products with filters
GET /api/products?category=Electronics&minPrice=50&maxPrice=200&sortBy=price-asc
# Search products
GET /api/products?search=headphones&rating=4
# Get paginated results
GET /api/products?page=2&limit=20curl -X POST http://localhost:3000/api/auth/register \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john@example.com",
"password": "password123"
}'curl -X POST http://localhost:3000/api/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "john@example.com",
"password": "password123"
}'curl -X GET http://localhost:3000/api/auth/me \
-H "Authorization: Bearer YOUR_JWT_TOKEN"Run the seeding script to populate your database with sample data:
npm run seedThis creates:
- 3 sample users (including 1 admin)
- 5 sample products with various categories
- Admin credentials:
admin@Heritage spparow.com/admin123456
GET /healthLogs are stored in the logs/ directory:
error.log: Error logs onlycombined-YYYY-MM-DD.log: All logs with daily rotation
/health: Application health status/api: API documentation and status
- JWT Authentication: Stateless authentication
- Rate Limiting: Prevents abuse and DDoS attacks
- Input Validation: Request validation using express-validator
- SQL Injection Protection: MongoDB sanitization
- XSS Protection: Input sanitization
- CORS Configuration: Controlled cross-origin requests
- Helmet: Security headers
- Parameter Pollution: HPP protection
# Install PM2 globally
npm install -g pm2
# Start application
NODE_ENV=production pm2 start server.js --name Heritage spparow-backend
# Auto-restart on system reboot
pm2 startup
pm2 save
# Monitor and manage
pm2 status
pm2 logs Heritage spparow-backend
pm2 restart Heritage spparow-backend- Heroku: Git-based deployment with Procfile
- AWS: EC2 instances with PM2
- Google Cloud: Compute Engine or App Engine
- Digital Ocean: Droplets with PM2
- Railway: Direct GitHub deployment
- Render: Auto-deploy from Git
# 1. Clone repository on your server
git clone <your-repo-url>
cd Heritage spparow/server
# 2. Install dependencies
npm install --production
# 3. Set up environment
cp .env.example .env
# Edit .env with your production values
# 4. Install PM2
npm install -g pm2
# 5. Start application
NODE_ENV=production pm2 start server.js --name Heritage spparow-backend
# 6. Set up auto-restart
pm2 startup
pm2 save
# 7. Optional: Set up Nginx reverse proxy
# Configure Nginx to proxy requests to localhost:3000- Development:
NODE_ENV=development - Staging:
NODE_ENV=staging - Production:
NODE_ENV=production
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions:
- Create an issue in the repository
- Check the API documentation at
/api - Review the logs in the
logs/directory
- v1.0.0: Initial release with core ecommerce features
- v1.1.0: Added payment integration and enhanced security
- v1.2.0: PM2 support and production optimizations