express-http-proxy-2 is an HTTP proxy middleware for the Express.js framework, designed to forward incoming requests to a specified target host and stream the responses back. This package is a community-maintained fork of the original `express-http-proxy` library, specifically created to address critical bugs, such as issue #509, and to provide comprehensive TypeScript type definitions, making it more robust for modern JavaScript and TypeScript projects. Currently at version 1.1.0, its release cadence is tied to bug fixes and feature enhancements derived from the community's needs, rather than a fixed schedule. Key differentiators include built-in support for streaming requests and responses, the ability to use Promises for asynchronous hooks, and flexible host selection which can be a static string or a dynamic function evaluated per request. It seamlessly integrates into Express applications, offering various configuration options to customize request path resolution, header manipulation, and conditional proxying.
npm install express-http-proxy-2Verified import paths — ran on the pinned version, not inferred.
Demonstrates setting up `express-http-proxy-2` to proxy requests from `/api` to an external JSON API, including path rewriting and response modification.
Migrate any usage of `forwardPath` or `forwardPathAsync` to the `proxyReqPathResolver` function, which supports both synchronous and Promise-based asynchronous operations.
Ensure that `app.use(proxy(...))` is called before `app.use(bodyParser.json())` or similar body parsing middleware for routes that will be proxied.
Be mindful of performance implications when using response decorators with large data transfers. Only use these options when necessary, or consider alternative approaches if high-performance streaming is critical for large files.
For CommonJS, use `const proxy = require('express-http-proxy-2');`. For ESM, use `import proxy from 'express-http-proxy-2';`. Ensure your `tsconfig.json` or build setup is correctly configured for your module system.Review any custom hooks (e.g., `proxyErrorHandler`, `userResDecorator`) to ensure they handle errors or final responses without inadvertently calling `res.send()` or `res.end()` more than once, or after the proxy has already begun sending data.
First, verify the `host` argument is correct and accessible. Then, meticulously debug `proxyReqPathResolver` by logging the `req.url` and the `return` value to ensure the target path is constructed as expected. Check if the `filter` option is unintentionally blocking requests.
No dependency data recorded yet.