This is a simple proxy server built using Express.js, Node.js, and NodeCache. It forwards incoming requests to an origin server and caches the responses to improve performance and reduce load on the origin server.
Project from Backend Learning Path - Caching Server
- Proxy requests to a specified origin server.
- Cache responses with a configurable Time-to-Live (TTL).
- Clear the cache using a command-line flag.
- Configurable via command-line arguments.
- Supports all HTTP methods and forwards headers, body, and query parameters.
- Node.js (>= 14.x recommended)
- npm or yarn
# Install globally
npm install -g .
# Or run directly with
npm startStart the proxy server:
caching-proxy --port <number> --origin <url> [--ttl <seconds>]Example:
caching-proxy --port 3000 --origin http://dummyjson.com --ttl 7200Clear the cache:
caching-proxy --clear-cache--port,-p: Port to run the proxy server on--origin,-o: Origin server URL to proxy requests to--ttl,-t: Cache TTL in seconds (default: 3600)--clear-cache: Clear the cache and exit--help: Show help
The proxy adds an X-Cache header to indicate cache status:
X-Cache: HIT- Response served from cacheX-Cache: MISS- Response from origin server
- Configurable Cache TTL: Allows setting a time-to-live (TTL) for cached responses via command-line arguments.
- Cache Clearing: Supports a --clear-cache flag to flush all cached data and exit.
- Command-Line Configuration: Flexible command-line options for setting port, origin, TTL, and other runtime parameters.
- Full HTTP Method Support: Handles all HTTP methods, including GET, POST, PUT, DELETE, and more.
- Error Handling: Provides meaningful error messages for bad gateway errors or origin server failures.