A simple HTTP proxy server written in Node.js that forwards requests to an origin server, follows redirects, and caches responses on disk for faster subsequent access.
- π Redirect handling: Automatically follows
301,302,303,307, and308redirects. - πΎ Response caching: Stores responses in a local
cache-filesdirectory for reuse. - β‘ Cache hit optimization: Serves cached responses instantly with a custom
X-Cache: HITheader. - π§Ή Cache clearing: Supports a
--clear-cacheflag to delete all cached files. - π‘οΈ Error handling: Returns appropriate status codes for too many redirects or bad gateways.
Clone the repo and install dependencies:
git clone https://github.com/your-username/proxy-cache-server.git
cd proxy-cache-server
npm install
## π Usage
Run the server with:
```bash
node index.js --port=3000 --origin=http://dummyjson.com --redirect=10-
--port <number>Port to run the proxy server on. Default:3000. -
--origin <url>Origin server to forward requests to. Default:http://dummyjson.com. -
--redirectThe number of allowed redirections -
--clear-cacheClears the cache and exits.
Start server on port 4000:
node index.js --port=4000 --origin=https://example.comClear cache:
node index.js --clear-cache.
βββ index.js # Main proxy server
βββ utils/
β βββ cache-functions.js # Cache management utilities
βββ cache-files/ # Cached responses (auto-created)
βββ package.json
-
Incoming requests are mapped to the origin server.
-
If a cached response exists, it is served immediately with headers restored.
-
If no cache is found:
- The request is proxied to the origin.
- Redirects are followed if necessary.
- The response is cached and sent back to the client.
Solution for Caching Proxy Project https://roadmap.sh/projects/caching-server