A configurable CLI-based caching proxy server that forwards HTTP requests and caches responses for improved performance.
npm install
chmod +x index.js
npm linkcaching-proxy --port 3000 --origin http://dummyjson.comcaching-proxy --clear-cacheCreate a default configuration file:
caching-proxy --init-configThis creates proxy-config.json in your current directory with default settings.
caching-proxy --show-configEdit proxy-config.json to customize behavior:
{
"cache": {
"maxSize": 100, // Maximum cached entries (prevents unlimited growth)
"ttl": 3600000, // Time to live in milliseconds (1 hour = 3600000)
"enabled": true, // Enable/disable caching
"cacheFile": "cache-data.json", // Cache storage file name
"cleanupInterval": 300000 // Auto-cleanup interval in ms (5 min)
},
"server": {
"timeout": 30000, // Request timeout in ms (30 seconds)
"followRedirects": true, // Follow HTTP redirects
"maxRedirects": 5 // Maximum redirects to follow
},
"logging": {
"verbose": false, // Enable verbose logging
"logRequests": true, // Log incoming requests
"logCache": true // Log cache hits/misses
}
}CLI arguments override configuration file settings:
# Override max cache size
caching-proxy --port 3000 --origin http://dummyjson.com --max-size 50
# Override TTL (in seconds)
caching-proxy --port 3000 --origin http://dummyjson.com --ttl 1800
# Enable verbose logging
caching-proxy --port 3000 --origin http://dummyjson.com --verbose
# Disable caching (proxy-only mode)
caching-proxy --port 3000 --origin http://dummyjson.com --no-cache
# Override request timeout (in seconds)
caching-proxy --port 3000 --origin http://dummyjson.com --timeout 60- Command-line arguments (highest priority)
- Configuration file (
proxy-config.json) - Default values (lowest priority)
| Duration | Milliseconds | Seconds |
|---|---|---|
| 5 minutes | 300000 | 300 |
| 30 minutes | 1800000 | 1800 |
| 1 hour | 3600000 | 3600 |
| 6 hours | 21600000 | 21600 |
| 24 hours | 86400000 | 86400 |
caching-proxy --port 3000 --origin http://dummyjson.comcaching-proxy --port 3000 --origin http://dummyjson.com --verbose --ttl 300caching-proxy --port 3000 --origin http://dummyjson.com --max-size 500 --ttl 7200caching-proxy --port 3000 --origin http://dummyjson.com --no-cache# Start the proxy
caching-proxy --port 3000 --origin http://dummyjson.com
# In another terminal, make requests
curl -i http://localhost:3000/products
# First request: X-Cache: MISS
curl -i http://localhost:3000/products
# Second request: X-Cache: HIT
# View configuration
caching-proxy --show-config
# Clear cache
caching-proxy --clear-cacheThe proxy adds custom headers to responses:
X-Cache: HIT- Response served from cacheX-Cache: MISS- Response fetched from origin serverX-Cached-At- Timestamp when response was cached (only on HIT)
- TTL Expiration: Old entries are automatically removed after TTL expires
- Size Limit: When cache reaches
maxSize, least recently used entries are evicted - LRU Eviction: Frequently accessed data stays in cache longer
- Disk Persistence: Cache survives server restarts
# Clear all cache
caching-proxy --clear-cache
# View cache statistics (stop server with Ctrl+C to see stats)┌─────────┐ ┌─────────────┐ ┌────────────┐
│ Client │─────▶│ Proxy Server│─────▶│ Origin │
│ │◀─────│ + Cache │◀─────│ Server │
└─────────┘ └─────────────┘ └────────────┘
│
▼
┌──────────┐
│ Disk │
│ Storage │
└──────────┘
proxy-config.json- Configuration file (created with--init-config)cache-data.json- Cache storage (created automatically when caching)
# Error: Port 3000 is already in use
# Solution: Use a different port
caching-proxy --port 3001 --origin http://dummyjson.comThe cache persists to disk. Make sure:
- No proxy server is running
- Run
caching-proxy --clear-cache - Check if
cache-data.jsonis deleted
# Error: Bad Gateway - Could not reach origin server
# Check: Is the origin URL correct?
# Check: Is the origin server running?Edit proxy-config.json:
{
"cache": {
"cacheFile": "/path/to/custom-cache.json"
}
}{
"logging": {
"logRequests": false,
"logCache": false
}
}{
"server": {
"timeout": 60000 // 60 seconds
}
}MIT