http proxy middleware of koa2

Usage no npm install needed!

<script type="module">
  import chuxingpayKoaProxyMiddleware from 'https://cdn.skypack.dev/@chuxingpay/koa-proxy-middleware';



NPM version build status Test coverage

Middleware for koa2. Reverse proxy middleware for koa. Proxy resources on other servers, such as Java services, and other node.js applications. Based on http-proxy library.


node v8.x +




koa-proxy-middleware is used to forward HTTP requests in koa middleware,it looks like the nginx in Node.

const Koa = require('koa');
const Proxy = require('koa-proxy-middleware');
const app = new Koa();
const proxy = new Proxy({
  proxies: [
      host: 'http://localhost:3333/',
      context: 'nginx'



  • proxies
    koa-proxy-middleware config option,expect Array value,Each of the config objects is a proxy combination,you should fill this option with client require context prefix and host server address.

    • host url string to be parsed with the url module
    • context Local proxy root address,required,string format
    • rewrite unrequired,Function/Boolean value. It doesn't overwrite the path when it's false.
    • proxyTimeout unrequired,Number
  • proxyTimeout
    timeout(in millis) for outgoing proxy requests. unrequired,default 30000

  • rewrite
    rewrite the url redirects function.unrequired,default () => path.replace(context, '').It's unnecessary to replace '/' path because funciton rewrite did it default in http-proxy

  • logLevel
    Log level of terminal output,includes error, warn, info, http, verbose, debug, debug, silly, it dependence on Winston package

  • handleReq
    The function will be triggered before send data. you can modify the request object of request before handle proxy.This method takes four arguments proxyReq,req,res,options

const proxy = new Proxy({
  proxies: ...,
  handleReq: proxyObj => {
    { proxyReq, req, res, options } = proxyObj;
  • handleRes
    The function will be triggered if back response from the target server.It takes three arguments proxyRes,req,res

  • error
    The error function will be triggered when fail in request to the target server.It takes three arguments err,req,res