{"id":16829,"library":"http2-proxy","title":"HTTP/2 & HTTP/1.1 Proxy for Node.js","description":"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.","status":"active","version":"5.0.53","language":"javascript","source_language":"en","source_url":"https://github.com/nxtedition/node-http2-proxy","tags":["javascript","http2","http","proxy","typescript"],"install":[{"cmd":"npm install http2-proxy","lang":"bash","label":"npm"},{"cmd":"yarn add http2-proxy","lang":"bash","label":"yarn"},{"cmd":"pnpm add http2-proxy","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since v5.x, the package is primarily ESM-first. While CommonJS `require` might work for the default export, `import` is the recommended and safest approach for consistency across Node.js environments.","wrong":"const proxy = require('http2-proxy')","symbol":"proxy","correct":"import proxy from 'http2-proxy'"},{"note":"`web` is a method of the default `proxy` export, not a direct named export from the package.","wrong":"import { web } from 'http2-proxy'","symbol":"proxy.web","correct":"import proxy from 'http2-proxy'; proxy.web(req, res, options, handler)"},{"note":"`ws` is a method of the default `proxy` export, not a direct named export from the package.","wrong":"import { ws } from 'http2-proxy'","symbol":"proxy.ws","correct":"import proxy from 'http2-proxy'; proxy.ws(req, socket, head, options, handler)"}],"quickstart":{"code":"import http2 from 'http2';\nimport proxy from 'http2-proxy';\nimport finalhandler from 'finalhandler';\n\nconst port = 8000;\nconst targetHostname = 'localhost';\nconst targetPort = 9001; // Ensure a backend server is running on this port for testing\n\nconst defaultWebHandler = (err, req, res) => {\n  if (err) {\n    console.error('HTTP proxy error:', err.message); // Log error message for clarity\n    if (!res.headersSent) {\n      finalhandler(req, res)(err);\n    } else {\n      // If headers already sent, just end the response to prevent further errors\n      res.end();\n    }\n  }\n};\n\nconst defaultWsHandler = (err, req, socket, head) => {\n  if (err) {\n    console.error('WebSocket proxy error:', err.message);\n    socket.destroy();\n  }\n};\n\nconst server = http2.createServer({ allowHTTP1: true });\n\nserver.on('request', (req, res) => {\n  proxy.web(req, res, {\n    hostname: targetHostname,\n    port: targetPort,\n    // Optional: Add custom headers to the request sent to the target\n    onReq: (proxyReq, options) => {\n      options.headers['x-forwarded-for'] = req.socket.remoteAddress;\n      options.headers['x-custom-proxy-header'] = 'http2-proxy-example';\n    },\n    // Optional: Modify the response from the target before sending to client\n    onRes: (req, res, proxyRes) => {\n      res.setHeader('x-proxied-by', 'http2-proxy');\n      res.writeHead(proxyRes.statusCode, proxyRes.headers);\n      proxyRes.pipe(res);\n    }\n  }, defaultWebHandler);\n});\n\nserver.on('upgrade', (req, socket, head) => {\n  proxy.ws(req, socket, head, {\n    hostname: targetHostname,\n    port: targetPort\n  }, defaultWsHandler);\n});\n\nserver.listen(port, () => {\n  console.log(`Proxy server listening on port ${port}`);\n  console.log(`Proxying HTTP and WebSocket requests to ${targetHostname}:${targetPort}`);\n});","lang":"javascript","description":"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."},"warnings":[{"fix":"Upgrade your Node.js environment to version 10.0.0 or newer.","message":"http2-proxy requires Node.js v10.0.0 or higher. Running on older Node.js versions may lead to unexpected behavior or runtime errors due to missing API features.","severity":"gotcha","affected_versions":"<10.0.0"},{"fix":"Consult the official GitHub repository's release notes or changelog for specific migration instructions when upgrading across major versions.","message":"The significant jump from v1.x to v5.x likely introduced breaking changes in the API and internal implementation. While specific changes are not detailed in the provided README excerpt, users upgrading from v1.x should review release notes for a comprehensive list of breaking changes.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"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.","message":"Errored proxy responses are not automatically cleaned up. Users must implement explicit error handlers (like `finalhandler`) to prevent resource leaks and ensure proper client response, even for non-idempotent methods.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Refactor your proxy implementation to leverage Node.js's async/await syntax for better error handling and control flow.","message":"While callback-based API usage is still supported, it is officially discouraged in favor of async/await patterns. Relying heavily on callbacks may lead to less readable or harder-to-maintain code.","severity":"deprecated","affected_versions":">=1.1.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"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`.","cause":"This error often occurs when attempting to use CommonJS `require` to import the library, and then trying to access `web` as a named export directly from the module. The package's primary export is a default object containing `web` and `ws` methods, especially in an ESM context.","error":"TypeError: proxy.web is not a function"},{"fix":"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.","cause":"Proxy operations (especially `onReq` or `onRes` hooks) might involve asynchronous code that throws an error or returns a rejected promise, which is not caught or awaited, leading to an unhandled rejection.","error":"(node:XXXXX) UnhandledPromiseRejectionWarning: [error message]"}],"ecosystem":"npm","meta_description":null}