express-http-proxy is an Express.js middleware designed for proxying incoming HTTP requests to a specified target host. It offers extensive customization through hooks, enabling developers to modify incoming requests before they reach the proxy target and decorate outgoing responses before they are returned to the client. The package is currently at version 2.1.2 and is actively maintained, having received updates like the significant v2.0.0 release that upgraded underlying dependencies and ensured compatibility with modern Node.js environments (16, 18, and 20). While not on a strict, frequent release cadence, updates are issued to address compatibility and dependency needs, especially for critical Node.js versions. Its key differentiator lies in its comprehensive set of configurable hooks for request and response transformation, making it suitable for scenarios requiring fine-grained control over proxy behavior within an Express application, such as API gateways or hiding internal service endpoints.
npm install express-http-proxyVerified import paths — ran on the pinned version, not inferred.
This example sets up an Express server with `express-http-proxy` to forward requests from `/proxy` to `httpbin.org`. It demonstrates request/response logging, custom header injection, HTML response modification, and basic error handling.
Upgrade your Node.js runtime environment to version 16, 18, or 20 to ensure compatibility and stability.
Review existing `decorateUserRes` or similar response decorators to ensure they correctly handle streamed data or explicitly buffer the response data if necessary within the decorator.
Increase the timeout using the `timeout` option in `express-http-proxy`'s configuration, and also consider increasing `req.setTimeout()` on the Express request object if the issue persists on the client side. Ensure this timeout is sufficiently higher than the expected response time from the upstream service.
Configure Express's `body-parser` (or equivalent) with a higher `limit` option, e.g., `app.use(express.json({ limit: '50mb' }));`. Additionally, ensure `express-http-proxy` itself is not implicitly buffering large bodies beyond configurable limits.Always sanitize and validate any user-provided hostnames or URLs used in the `proxyReqPathResolver` or the initial `proxy` function argument. Use a whitelist of allowed target hosts or implement robust input validation to prevent malicious redirection.
Ensure that your custom `decorateUserRes` or `proxyErrorHandler` functions do not attempt to send a response or modify headers on the `userRes` object if `express-http-proxy` has already done so. Use `res.headersSent` to check if headers have already been sent before attempting modification.
Verify that the target host specified in `proxy(host, options)` is correct and that the target server is running and accessible from the machine running your Express application. Check firewalls or network configurations if the target is external.
Install the type definitions: `npm install --save-dev @types/express-http-proxy`. Ensure your `tsconfig.json` includes `node_modules/@types` in its `typeRoots` or that the types are correctly picked up by your IDE.
Increase the `timeout` option within the `express-http-proxy` configuration, e.g., `proxy(host, { timeout: 120000 })` for 120 seconds. If the issue persists, examine the performance of the upstream service or network latency.