This package provides HTTP proxying capabilities for Koa 2 applications, serving as a middleware wrapper around the widely used `http-proxy-middleware` library. It enables developers to configure reverse proxies for routing requests to different target servers, perform path rewriting, and manage `changeOrigin` headers, which are common requirements for API gateways, development proxies, or microservice architectures. The library is currently at version 0.1.0 and has not seen any updates since its initial release in 2017, indicating it is no longer actively maintained. Its core proxying logic and options are derived directly from the underlying `http-proxy-middleware` dependency.
npm install koa-server-http-proxyVerified import paths — ran on the pinned version, not inferred.
Demonstrates setting up a basic HTTP proxy for a Koa application, showing single and multiple target configurations with path rewriting.
Migrate to actively maintained Koa proxy middleware solutions like `koa-proxies` (uses `http-proxy-middleware` v2.x/v3.x) or `@whitetrefoil/koa-http-proxy` (uses `node-http-proxy` directly).
Consult the documentation for `http-proxy-middleware` version `0.17.4` specifically, or, preferably, migrate to a more current Koa proxy solution that uses a recent `http-proxy-middleware` version.
Ensure that `koa-server-http-proxy` (or any proxy middleware) is applied before any middleware that consumes the request body, such as `koa-bodyparser` or `koa-body`. Process the body *after* the proxy has decided not to handle the request, or use `responseInterceptor` in modern `http-proxy-middleware` to handle bodies after proxying.
Ensure that Koa's context (`ctx.respond = false`) is correctly managed if the proxy middleware is intended to fully handle the response, preventing Koa from attempting to send its own headers. Check for other middleware that might be sending responses prematurely. Sometimes, older proxy libraries have specific workarounds for Koa's `res.end()` behavior.
Ensure `changeOrigin: true` is set in the proxy options to correctly set the `Host` header of the proxy request to the target's host. If the target API still requires specific CORS headers, you may need to add `proxyRes` event listeners to modify the response headers coming *from* the proxied server before they reach the client, or configure CORS on the target API itself if possible.