cache-proxy-server
v2.0.6
Published
A simple HTTP caching proxy server that forwards requests to localhost:3000 with intelligent in-memory caching
Maintainers
Readme
Cache Server Proxy
A caching proxy server that forwards requests to a target server and caches responses for improved performance.
Overview
This proxy server acts as an intermediary between clients and a target server. It caches responses based on the request method, URL, and body, reducing redundant requests to the target server.
Features
- 🚀 Request Forwarding: Proxies all HTTP requests to the target server
- 💾 Smart Caching: Caches responses with unique keys based on request parameters
- ⚡ Performance: Returns cached responses instantly for duplicate requests
- 🔄 All HTTP Methods: Supports GET, POST, PUT, PATCH, and other HTTP methods
- 📝 Request Logging: Logs cache hits and proxy requests for debugging
Installation
Install as a CLI Tool (Recommended)
npm install -g cache-proxy-serverThen start the server anywhere:
cache-proxy-serverInstall from Source
git clone https://github.com/nirtal/cache-server-proxy.git
cd cache-server-proxy
npm installUsage
CLI Usage (Global Installation)
The proxy server requires two arguments: --from (where the proxy listens) and --target (where requests are forwarded).
cache-proxy-server --from http://localhost:3100 --target http://localhost:3000Options:
--from <url>- URL to run the proxy server on (required)--target <url>- URL to forward requests to (required)-h, --help- Show help message-v, --version- Show version number
Examples:
# Basic usage with default ports
cache-proxy-server --from http://localhost:3100 --target http://localhost:3000
# Use different ports
cache-proxy-server --from http://localhost:8080 --target http://localhost:3000
# Forward to a remote server
cache-proxy-server --from http://0.0.0.0:3100 --target http://api.example.com
# Forward to a different host and port
cache-proxy-server --from http://localhost:4000 --target http://staging-server:8080Source Usage
If running from source, you can modify the default values in index.js or use the CLI interface:
# Run with default values (http://localhost:3100 -> http://localhost:3000)
npm start
# Or use node directly
node bin/cli.js --from http://localhost:3100 --target http://localhost:3000Development Mode
For development with auto-reload:
npm run devConfiguration
The proxy server is configured via command-line arguments:
--from <url>: Specifies where the proxy server listens (e.g.,http://localhost:3100)--target <url>: Specifies where requests are forwarded (e.g.,http://localhost:3000)
Both arguments are required when using the CLI. When running from source without arguments, it uses default values:
- Default Proxy:
http://localhost:3100 - Default Target:
http://localhost:3000
Programmatic Usage
You can also use the proxy server as a module in your own code:
import { createProxyServer } from 'cache-proxy-server';
createProxyServer('http://localhost:3100', 'http://localhost:3000');How It Works
- Request Reception: Client sends a request to the proxy server (configured via
--from) - Cache Check: Server generates a unique cache key based on:
- HTTP method
- Request URL
- Request body
- Cache Hit: If cached, returns the stored response immediately
- Cache Miss: If not cached:
- Forwards request to target server (configured via
--target) - Stores response in cache
- Returns response to client
- Forwards request to target server (configured via
Cache Key Generation
Cache keys are generated using SHA1 hash of:
{
method: "GET",
url: "/api/endpoint",
body: { ... }
}Example
Assuming you started the proxy with:
cache-proxy-server --from http://localhost:3100 --target http://localhost:3000Then make requests:
# First request - proxied to target server
curl http://localhost:3100/api/data
# Second identical request - served from cache
curl http://localhost:3100/api/dataConsole output:
[PROXY] GET http://localhost:3000/api/data
[CACHE HIT] GET /api/dataDependencies
- express: Web framework for handling HTTP requests
- node-fetch: HTTP client for forwarding requests
- crypto: Built-in Node.js module for hash generation
Development Dependencies
- nodemon: Auto-restart server during development
Client-Side Setup with Requestly
To enable this proxy on client sites, you need to use the Requestly Chrome Extension.
Setup Instructions
Install Requestly
- Download and install the Requestly Chrome Extension
Configure Script Injection
- Create a new rule in Requestly
- Set the rule type to "Insert Scripts"
- Configure the URL condition based on your client application (e.g., URLs containing
http://localhost:4200) - Add the following script (adjust URLs to match your setup):
<script type="text/javascript">
(function() {
// Configure these URLs to match your setup
const originalTarget = "http://localhost:3000"; // The original API server
const proxyUrl = "http://localhost:3100"; // Your proxy server (--from URL)
// Intercept all fetch requests
const originalFetch = window.fetch;
window.fetch = async function(resource, config) {
if (typeof resource === "string" && resource.startsWith(originalTarget)) {
const redirected = resource.replace(originalTarget, proxyUrl);
console.log("[Requestly Redirect] fetch →", redirected);
return originalFetch(redirected, config);
}
return originalFetch(resource, config);
};
// Intercept XMLHttpRequests
const OriginalXHR = window.XMLHttpRequest;
function RedirectedXHR() {
const xhr = new OriginalXHR();
const originalOpen = xhr.open;
xhr.open = function(method, url, ...rest) {
if (url && typeof url === "string" && url.startsWith(originalTarget)) {
const redirected = url.replace(originalTarget, proxyUrl);
console.log("[Requestly Redirect] XHR →", redirected);
return originalOpen.call(this, method, redirected, ...rest);
}
return originalOpen.call(this, method, url, ...rest);
};
return xhr;
}
window.XMLHttpRequest = RedirectedXHR;
// Intercept navigation (window.location etc.)
const originalOpen = window.open;
window.open = function(url, ...args) {
if (url && typeof url === "string" && url.startsWith(originalTarget)) {
url = url.replace(originalTarget, proxyUrl);
console.log("[Requestly Redirect] window.open →", url);
}
return originalOpen.call(window, url, ...args);
};
console.log(`✅ Requestly redirect script loaded: ${originalTarget} → ${proxyUrl}`);
})();
</script>- How It Works
- The script intercepts all HTTP requests made from your client application
- Automatically redirects requests from the original API server to your proxy server
- Logs all redirected requests in the browser console for debugging
- Note: Adjust
originalTargetandproxyUrlin the script to match your--targetand--fromURLs
Error Handling
If the target server is unreachable or returns an error, the proxy will return a 502 Bad Gateway status with error details.
License
ISC
Notes
- Cache is stored in memory and will be cleared when the server restarts
- All request headers are forwarded to the target server
- Response headers from the target server are preserved in cached responses
