The Last Mile Delivery Platform is now ready for deployment with comprehensive API versioning, backward compatibility, and all core features implemented.
- β User Authentication & Role Management (V1 & V2)
- β Enhanced Offer Management with geospatial queries
- β Location Services with real-time tracking
- β Notification System (multi-channel support)
- β Payment Processing & earnings tracking
- β Delivery Tracking with real-time updates
- β Error Handling & comprehensive validation
- β Admin Dashboard & monitoring
- β Performance Optimization with Redis caching
- β Role-Specific Dashboard APIs
- β API Versioning & backward compatibility
- V1.0: Current stable version with backward compatibility
- V2.0: Enhanced version with advanced security and features
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Frontend β β API Gateway β β Backend β
β (React) βββββΊβ (Express) βββββΊβ (Node.js) β
β β β - Versioning β β - Business β
β β β - Auth β β - Services β
β β β - Rate Limit β β - Models β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β
βΌ
βββββββββββββββββββ βββββββββββββββββββ
β Database β β Cache β
β (MongoDB) β β (Redis) β
β - Users β β - Sessions β
β - Offers β β - Offers β
β - Payments β β - Locations β
βββββββββββββββββββ βββββββββββββββββββ
Create a .env file with the following variables:
# Database
MONGO_URI=mongodb://localhost:27017/lastmile
REDIS_URL=redis://localhost:6379
# Authentication
JWT_SECRET=your-super-secure-jwt-secret-key-here
JWT_REFRESH_SECRET=your-refresh-token-secret-key-here
# API Configuration
NODE_ENV=production
PORT=5000
# External Services (Optional)
GEOCODING_API_KEY=your-geocoding-api-key
EMAIL_SERVICE_API_KEY=your-email-service-key
PAYMENT_GATEWAY_API_KEY=your-payment-gateway-key
# Security
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX_REQUESTS=100
# Caching
CACHE_TTL=3600
CACHE_MAX_SIZE=1000# Backend dependencies
npm install
# Frontend dependencies (if deploying frontend)
cd frontend
npm install
cd ..-
Prepare for Heroku
# Install Heroku CLI # Create Procfile (already exists) echo "web: node server.js" > Procfile
-
Deploy to Heroku
# Login to Heroku heroku login # Create Heroku app heroku create your-app-name # Add MongoDB Atlas addon heroku addons:create mongolab:sandbox # Add Redis addon heroku addons:create heroku-redis:hobby-dev # Set environment variables heroku config:set JWT_SECRET=your-jwt-secret heroku config:set NODE_ENV=production # Deploy git add . git commit -m "Deploy to Heroku" git push heroku main
-
Configure Database
# Get MongoDB connection string heroku config:get MONGODB_URI # Get Redis URL heroku config:get REDIS_URL
-
EC2 Instance Setup
# Launch EC2 instance (Ubuntu 20.04 LTS) # SSH into instance ssh -i your-key.pem ubuntu@your-ec2-ip # Install Node.js curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt-get install -y nodejs # Install PM2 for process management sudo npm install -g pm2 # Install MongoDB wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add - echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list sudo apt-get update sudo apt-get install -y mongodb-org # Install Redis sudo apt-get install redis-server
-
Deploy Application
# Clone repository git clone your-repo-url cd lastmile-delivery-platform # Install dependencies npm install # Set up environment variables cp .env.example .env # Edit .env with your values # Start with PM2 pm2 start server.js --name "lastmile-api" pm2 startup pm2 save
-
Configure Nginx (Optional)
# Install Nginx sudo apt-get install nginx # Configure reverse proxy sudo nano /etc/nginx/sites-available/lastmile
server { listen 80; server_name your-domain.com; location / { proxy_pass http://localhost:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_cache_bypass $http_upgrade; } }
-
Create Docker Configuration
# Dockerfile (create this file) FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm ci --only=production COPY . . EXPOSE 5000 CMD ["node", "server.js"]
-
Docker Compose Setup
# docker-compose.yml (create this file) version: '3.8' services: app: build: . ports: - "5000:5000" environment: - NODE_ENV=production - MONGO_URI=mongodb://mongo:27017/lastmile - REDIS_URL=redis://redis:6379 depends_on: - mongo - redis mongo: image: mongo:6.0 ports: - "27017:27017" volumes: - mongo_data:/data/db redis: image: redis:7-alpine ports: - "6379:6379" volumes: mongo_data:
-
Deploy with Docker
# Build and run docker-compose up -d # View logs docker-compose logs -f app
- β Environment Variables: All secrets in environment variables
- β JWT Security: Strong secret keys with proper expiration
- β Rate Limiting: Configured for production traffic
- β Input Validation: Comprehensive validation middleware
- β CORS Configuration: Properly configured for your domain
- β HTTPS: Enable SSL/TLS in production
- β Database Security: MongoDB authentication enabled
- β Error Handling: No sensitive data in error responses
// Add to server.js for production
if (process.env.NODE_ENV === 'production') {
// Security headers
app.use(helmet());
// Trust proxy for Heroku/AWS
app.set('trust proxy', 1);
// Secure session configuration
app.use(session({
secret: process.env.SESSION_SECRET,
secure: true, // HTTPS only
httpOnly: true,
maxAge: 24 * 60 * 60 * 1000 // 24 hours
}));
}-
Health Check Endpoint
// Add to server.js app.get('/health', (req, res) => { res.json({ status: 'healthy', timestamp: new Date().toISOString(), version: process.env.npm_package_version, uptime: process.uptime() }); });
-
Logging Configuration
# Install logging dependencies npm install winston morgan -
Performance Monitoring
- Use PM2 monitoring for Node.js metrics
- Set up MongoDB monitoring
- Configure Redis monitoring
- Set up error tracking (e.g., Sentry)
# Test version endpoints
curl https://your-domain.com/api/version
# Test authentication
curl -X POST https://your-domain.com/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{"name":"Test User","email":"test@example.com","password":"TestPass123!","role":"business"}'
# Test versioning
curl https://your-domain.com/api/v1/version
curl https://your-domain.com/api/v2/version# Install artillery for load testing
npm install -g artillery
# Create load test configuration
# artillery-config.yml
config:
target: 'https://your-domain.com'
phases:
- duration: 60
arrivalRate: 10
scenarios:
- name: "API Health Check"
requests:
- get:
url: "/health"- Current Version: V1 (stable)
- Beta Version: V2 (enhanced features)
- Deprecation Policy: 6 months notice before sunset
- Migration Support: Automatic compatibility layer
// JavaScript client example
const API_BASE = 'https://your-domain.com/api';
// V1 client (backward compatible)
const v1Client = {
baseURL: `${API_BASE}/v1`,
headers: { 'Accept-Version': 'v1' }
};
// V2 client (enhanced features)
const v2Client = {
baseURL: `${API_BASE}/v2`,
headers: { 'Accept-Version': 'v2' }
};-
Load Balancer Configuration
- Use Nginx or AWS ALB
- Session affinity for WebSocket connections
- Health check configuration
-
Database Scaling
- MongoDB replica sets
- Read replicas for analytics
- Sharding for large datasets
-
Cache Scaling
- Redis Cluster for high availability
- Cache warming strategies
- Cache invalidation patterns
- Database Indexing: All geospatial and frequently queried fields
- Redis Caching: User sessions, offer data, location data
- CDN: Static assets and API responses
- Compression: Gzip compression enabled
- Connection Pooling: MongoDB and Redis connection pools
-
Database Connection Issues
# Check MongoDB status sudo systemctl status mongod # Check connection string echo $MONGO_URI
-
Redis Connection Issues
# Check Redis status redis-cli ping # Check Redis configuration redis-cli config get "*"
-
API Version Issues
# Test version detection curl -H "Accept-Version: v1" https://your-domain.com/api/version curl -H "Accept-Version: v2" https://your-domain.com/api/version
# PM2 logs
pm2 logs lastmile-api
# Application logs
tail -f logs/app.log
# MongoDB logs
tail -f /var/log/mongodb/mongod.log
# Redis logs
tail -f /var/log/redis/redis-server.log-
Database Backups
# MongoDB backup mongodump --uri="$MONGO_URI" --out=/backup/$(date +%Y%m%d)
-
Redis Backups
# Redis backup redis-cli BGSAVE
-
Rolling Updates
- Deploy to staging first
- Test all API versions
- Gradual production rollout
- Monitor error rates
-
Rollback Plan
- Keep previous version ready
- Database migration rollback scripts
- Quick rollback procedures
Your Last Mile Delivery Platform is now ready for production with:
- β Comprehensive API versioning
- β Backward compatibility
- β Production-ready security
- β Scalable architecture
- β Monitoring and logging
- β Complete feature set
- Choose your deployment option
- Configure environment variables
- Set up monitoring
- Test all API endpoints
- Configure domain and SSL
- Set up automated backups
- Monitor performance and scale as needed
Your platform is ready to connect businesses with riders for efficient last-mile delivery! ππ¦