Express Request Proxy
express-request-proxy is an Express.js middleware designed for creating high-performance streaming HTTP reverse proxies. It enables features like transparent caching via Redis, server-side injection of sensitive API keys and headers, custom route parameter handling, and response transformations. The package is built upon the widely used, but now deprecated, `request` HTTP client library. Its last major release was 2.x, with version 2.2.2 being the latest. Given its dependency on the unmaintained `request` library and lack of recent updates (last commit August 2018), the package is effectively abandoned, limiting its viability for new projects and posing potential maintenance and security risks.
Common errors
-
TypeError: requestProxy is not a function
cause Attempting to use ES module import syntax (e.g., 'import') for a CommonJS-only package.fixChange your import statement to CommonJS: 'const requestProxy = require("express-request-proxy");' -
Error: Cannot find module 'request'
cause The 'request' dependency might be missing, or there are module resolution issues related to its deprecation and potential removal from package managers.fixEnsure 'request' is listed in your package.json and installed. However, due to its deprecation, it's strongly recommended to migrate away from this package. -
Redis Client Error connect ECONNREFUSED
cause The Redis server is not running or is inaccessible at the configured host/port, or the client was not connected before being passed to the proxy.fixVerify your Redis server is running and configured correctly. Check the host and port in 'redis.createClient()' options, and ensure `redisClient.connect()` has been called and awaited. -
TypeError: app.use() requires middleware functions but got a [object Object]
cause The configuration object passed to 'requestProxy' is invalid or incomplete, preventing it from returning a valid Express middleware function. Most commonly, the 'url' option is missing.fixEnsure the 'url' option is always provided in the 'requestProxy' configuration object, as it is the only required option.
Warnings
- breaking The core dependency, 'request' library, has been officially deprecated and is no longer maintained. This means 'express-request-proxy' inherits significant security vulnerabilities, stability issues, and lack of future updates. Using this package in production is highly discouraged.
- gotcha This package is CommonJS (CJS) only. Attempting to import it using ES module syntax (e.g., 'import requestProxy from "express-request-proxy"') will lead to runtime errors.
- gotcha The caching feature relies on a 'redis' client that must be extended with 'redis-streams'. Incorrect setup (e.g., missing 'require("redis-streams")(redis);') will prevent caching from working or cause runtime errors.
- gotcha Environment variables for sensitive keys (e.g., 'SOMEAPI_SECRET_KEY') must be set in the server environment before the application starts, as 'express-request-proxy' accesses them directly via 'process.env'.
- deprecated The 'request' library, which this package is built upon, is deprecated. Its API patterns (e.g., callback-based) are outdated compared to modern promise-based or async/await HTTP clients, affecting code maintainability and future compatibility.
Install
-
npm install express-request-proxy -
yarn add express-request-proxy -
pnpm add express-request-proxy
Imports
- requestProxy
import requestProxy from 'express-request-proxy';
const requestProxy = require('express-request-proxy');
Quickstart
const express = require('express');
const redis = require('redis');
const requestProxy = require('express-request-proxy');
require('redis-streams')(redis); // Extend redis client with streams functionality
const app = express();
const port = 3000;
// Create a Redis client for caching
const redisClient = redis.createClient();
redisClient.on('error', (err) => console.error('Redis Client Error', err));
// Connect to Redis (async operation)
async function connectRedis() {
await redisClient.connect();
console.log('Connected to Redis');
}
connectRedis();
app.get(
'/api/:resource/:id',
requestProxy({
cache: redisClient, // Use the connected Redis client
cacheMaxAge: 60, // Cache for 60 seconds
url: 'https://jsonplaceholder.typicode.com/:resource/:id', // Example public API
query: {
api_key: process.env.SOMEAPI_SECRET_KEY ?? 'demo-key' // Inject sensitive key from env
},
headers: {
'X-Custom-Auth': process.env.SOMEAPI_CUSTOM_HEADER ?? 'secret-value' // Inject custom header
}
})
);
app.get('/', (req, res) => {
res.send('Proxy server running. Try /api/posts/1 or /api/users/2');
});
app.listen(port, () => {
console.log(`Proxy server listening at http://localhost:${port}`);
console.log('Test with: curl http://localhost:3000/api/posts/1');
});