koa-better-http-proxy is an HTTP proxy middleware for Koa applications, enabling requests to be forwarded to a different host and the response passed back to the client. The current stable version is 0.2.10. This package has a moderate release cadence, with several minor updates and bug fixes released within the last year, indicating active maintenance. Key differentiators include its foundation on `express-http-proxy`, support for asynchronous path resolvers (`proxyReqPathResolver`) and response/header decorators (`userResDecorator`, `userResHeadersDecorator`), and automatic handling of gzipped responses. It provides fine-grained control over request and response modification, header stripping, and custom `http.Agent` usage, making it suitable for complex proxying scenarios within a Koa ecosystem.
npm install koa-better-http-proxyVerified import paths — ran on the pinned version, not inferred.
This example sets up a Koa server with `koa-better-http-proxy` to proxy requests starting with `/api` to `httpbin.org/anything`. It demonstrates path rewriting using `proxyReqPathResolver` and modifying the response body with `userResDecorator`.
Remove the `memoizeHost` option from your proxy configuration. There is no direct replacement as its functionality was not viable.
Upgrade to version 0.2.9 or higher to receive the fix for header isolation.
Use the `proxyReqPathResolver` option to modify the request path before it is sent to the proxied host. `proxyReqOptDecorator` is intended for modifying other request options like headers or agent settings.
If `Connection: close` is required, manually add it using the `headers` option or `proxyReqOptDecorator`: `headers: { 'Connection': 'close' }`.Avoid mutating `proxyRes` or `ctx` directly within decorators. Rely on dedicated options or hooks for header modification when they become available to ensure future compatibility. Focus `userResDecorator` on modifying response data and `userResHeadersDecorator` on modifying headers.
For ES Modules, use `import proxy from 'koa-better-http-proxy';`. For CommonJS, use `const proxy = require('koa-better-http-proxy');`.Ensure you have `import Koa from 'koa';` and `const app = new Koa();` at the start of your Koa application setup, and that `koa` is installed (`npm install koa`).
Ensure `koa-better-http-proxy` is at least v0.2.2 for TypeScript types, and that `koa` and `@types/koa` are correctly installed and aligned with your Koa version. Sometimes, reinstalling `@types/koa` can resolve such issues.
Install Koa: `npm install koa` or `yarn add koa`.