A simple Node.js proxy server built with Express that forwards requests to an origin server and caches responses with TTL (Time-To-Live).
- ✅ Forward requests to any origin server
- ⚡ In-memory caching for faster responses
- ⏱️ TTL-based cache expiration
- 💾 Persistent cache storage (
cache.json) - 🧹 Automatic cleanup of expired cache
- 🎯 CLI-based configuration (port, origin, ttl)
- 🔒 Caches only GET requests (best practice)
- Node.js
- Express
- Axios
- Yargs
- File System (
fs)
caching-proxy/
│── src/
│ └── index.js
│── cache.json
│── package.json
git clone <your-repo-url>
cd caching-proxy
npm installRun the server with:
npm start -- --port 3000 --origin http://dummyjson.com --ttl 60| Argument | Description |
|---|---|
--port |
Port to run the proxy server |
--origin |
Base URL of the origin server |
--ttl |
Cache time (in seconds) |
-
Client sends request to proxy
-
Proxy builds full target URL using origin
-
Cache is checked:
- ✅ If valid → return cached response
- ❌ If expired/missing → fetch from origin
-
Response is stored in cache with expiry
-
Cache is saved to
cache.json -
Response is sent back to client
GET /products
- ❌ Cache MISS
- Fetch from origin server
- Store in cache
GET /products
- ✅ Cache HIT
- Instant response
GET /products
- ⏰ Cache expired
- Fetch again from origin
- Runs automatically every 50 seconds
- Removes expired cache entries
- Updates
cache.json
Test route:
GET /home/api
- Only caches GET requests
- No cache size limit (can grow large)
- Uses file-based storage (not scalable)
- 🔄 Cache invalidation API (
/clear-cache) - 📦 LRU Cache (size-based eviction)
- ⚡ Redis integration (production-ready)
- 🧾 Cache headers support (ETag, Cache-Control)
This project is open-source.