362d2f94883c24170f3a009f2d3c99279c906adb
- Fix storage permissions to use 'www' user instead of 'www-data' - Fix UserModel getAllForAdmin query to avoid placeholders in LIMIT/OFFSET - Update deploy script to use correct user permissions - Clean up test files
MoneyFlix Redirector
High-performance NodeJS redirect service with MySQL analytics for the MoneyFlix CRM system.
🚀 Features
- High Performance: Handles millions of redirects with queue-based processing
- MySQL Analytics: Direct database logging with User Agent parsing
- Real-time Cache: 30-second redirect cache with automatic refresh
- Queue System: Batch processing with retry logic for reliability
- User Agent Analysis: Detailed device, browser, and OS detection
- Health Monitoring: Built-in status and health check endpoints
📁 Project Structure
moneyflix-redirector/
├── index.mjs # Main application entry point
├── db/
│ └── logUser.mjs # MySQL logging and queue system
├── helpers/
│ ├── helpers.mjs # CRM API integration
│ └── userAgentParser.mjs # User Agent parsing
├── ecosystem.config.js # PM2 configuration
├── package.json # Dependencies
└── README.md # This file
🛠️ Installation
-
Install dependencies:
npm install -
Configure environment:
cp env.example .env # Edit .env with your configuration -
Start with PM2:
pm2 start ecosystem.config.js
⚙️ Configuration
Environment Variables
| Variable | Default | Description |
|---|---|---|
NODE_ENV |
production |
Environment mode |
PORT |
3000 |
Server port |
CRM_API_URL |
https://redirect.moneyflix.com/api/redirects |
CRM API endpoint |
CRM_API_KEY |
moneyflix-api-key-2024 |
API authentication key |
DB_HOST |
127.0.0.1 |
MySQL host |
DB_PORT |
3306 |
MySQL port |
DB_USER |
root |
MySQL username |
DB_PASSWORD |
- | MySQL password |
DB_DATABASE |
bitreport_redirects |
MySQL database name |
QUEUE_MAX_SIZE |
1000 |
Maximum queue size |
QUEUE_FLUSH_INTERVAL |
5000 |
Queue flush interval (ms) |
CACHE_TTL |
30000 |
Redirect cache TTL (ms) |
🔗 API Endpoints
Redirect
GET /:slug
Redirects to the target URL for the given slug.
Health Check
GET /health
Returns service health status and basic metrics.
Status
GET /status
Returns detailed status including queue and database statistics.
📊 Database Schema
The service logs hits to the redirect_hits table:
CREATE TABLE redirect_hits (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
redirect_id BIGINT NOT NULL,
ip_address VARCHAR(45) NOT NULL,
user_agent TEXT,
referer TEXT,
destination_url TEXT NOT NULL,
response_time INT DEFAULT 0,
status VARCHAR(50) DEFAULT 'redirected',
geo VARCHAR(10) DEFAULT 'Unknown',
browser VARCHAR(100) DEFAULT 'Unknown',
os VARCHAR(100) DEFAULT 'Unknown',
device VARCHAR(50) DEFAULT 'Unknown',
is_mobile TINYINT(1) DEFAULT 0,
is_bot TINYINT(1) DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_redirect_id (redirect_id),
INDEX idx_ip_address (ip_address),
INDEX idx_created_at (created_at),
INDEX idx_redirect_created (redirect_id, created_at)
);
🚀 Performance Features
Queue System
- Batch Processing: Groups hits for efficient MySQL inserts
- Auto Flush: Processes queue every 5 seconds or when full
- Retry Logic: Failed hits are retried up to 3 times
- Memory Efficient: Configurable queue size limits
Caching
- Redirect Cache: 30-second TTL for redirect definitions
- Auto Refresh: Background updates from CRM API
- Memory Efficient: Only stores active redirects
User Agent Analysis
- Device Detection: Mobile, tablet, desktop classification
- Browser Parsing: Name, version, and engine detection
- OS Detection: Operating system and version parsing
- Bot Detection: Automatic crawler and bot identification
📈 Monitoring
Queue Statistics
{
queueSize: 42,
isProcessing: false,
maxSize: 1000,
flushInterval: 5000
}
Database Statistics
{
total_hits: 15420,
unique_redirects: 15,
unique_ips: 8923,
mobile_hits: 8234,
bot_hits: 234,
last_hit: "2024-01-15T10:30:45.000Z"
}
🔧 Development
Local Development
npm run dev
Production Deployment
npm start
# or
pm2 start ecosystem.config.js
Testing
# Test redirect
curl http://localhost:3000/mf_bonus
# Check health
curl http://localhost:3000/health
# Check status
curl http://localhost:3000/status
📝 Logs
Logs are stored in the logs/ directory:
combined.log- All logsout.log- Standard outputerror.log- Error logs
🚨 Error Handling
- Graceful Shutdown: Flushes remaining hits on SIGTERM/SIGINT
- Connection Pooling: Automatic reconnection to MySQL
- API Fallback: Continues working even if CRM API is down
- Queue Recovery: Retries failed database operations
📊 Analytics Data
Each redirect hit includes:
- Basic Info: IP, User Agent, Referer, Response Time
- Device Data: Browser, OS, Device Type, Mobile Detection
- Bot Detection: Automatic crawler identification
- Geographic: Country code (if available)
- Performance: Response time and status
Built by BitReport for MoneyFlix CRM System
Languages
JavaScript
67.7%
PHP
11.9%
SCSS
11.5%
CSS
7.6%
TypeScript
0.7%
Other
0.5%