http2-proxy is a robust Node.js library engineered to serve as a high-performance proxy for both HTTP/2 and HTTP/1.1 traffic, including support for WebSocket connections. Currently at version 5.0.53, the library maintains an active development status, though its major version release cadence appears irregular, with a significant jump from 1.x to 5.x. A core differentiator is its adherence to HTTP specifications, automatically managing critical headers such as hop-by-hop, connection, via, and forward. It is designed to be fully compatible with Node.js's async/await paradigm, with callback-based usage being an optional but discouraged alternative. A notable feature is its resilience during 503 errors, where it's safe to assume no data was read or written, enabling reliable request retries for all methods, including non-idempotent ones. Users are responsible for implementing their own final and error handlers, as the library does not perform automatic cleanup of errored responses, which provides flexibility for custom retry mechanisms. It requires Node.js v10.0.0 or higher to function correctly. The package also integrates seamlessly with common middleware frameworks like Connect and security libraries like Helmet.
npm install http2-proxyVerified import paths — ran on the pinned version, not inferred.
This code sets up an HTTP/2 server that also accepts HTTP/1.1 connections. It then proxies both HTTP and WebSocket requests to a specified target hostname and port, demonstrating error handling and optional request/response modification.
Upgrade your Node.js environment to version 10.0.0 or newer.
Consult the official GitHub repository's release notes or changelog for specific migration instructions when upgrading across major versions.
Provide a callback function to `proxy.web` and `proxy.ws` that handles errors, typically using `finalhandler` or a custom error handling middleware, to gracefully close connections or send error responses.
Refactor your proxy implementation to leverage Node.js's async/await syntax for better error handling and control flow.
When using ESM, use `import proxy from 'http2-proxy';`. When using CommonJS, if ESM default export is not properly handled, try `const { default: proxy } = require('http2-proxy');` or ensure your `require` statement assigns the default export to `proxy`.Ensure all asynchronous operations within `onReq`, `onRes`, or other custom handlers are properly awaited using `await` or handled with `.catch()` clauses. Additionally, confirm that your `defaultWebHandler` and `defaultWsHandler` are robustly catching and processing any errors passed to them by the proxy functions.
No dependency data recorded yet.