1.0.1 • Published 6 years ago

koa-any-proxy v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

koa-any-proxy

koa proxy http

Koa middlware for http proxy

Powered by http-proxy Powered by url-pattern

install

$ npm install koa-any-proxy --save

rules

// ***source*** , ***target*** 参考[`url-pattern`](https://www.npmjs.com/package/url-pattern) 规则
// ***options*** , 参考[`http-proxy`](https://github.com/nodejitsu/node-http-proxy) proxy.web(req,res,options)中的options 
[
  {// Rule (1)
    source : '/v:major(.:minor)/*',
    target : 'http://localhost:8080/cool', 
    options: {},//详见文档[options](https://github.com/nodejitsu/node-http-proxy#options)
    events:{ //详见文档[events](https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events)
      error:function(){},
      proxyReq:function(proxyReq, req, res, options){},
      proxyReqWs:function(){},
      proxyRes:function(proxyRes, req, res){},
      open:function(proxySocket){},
      close:function(err, req, res){},
    }
  },
  {// Rule (2)
    source:'/api/users/:ids/posts/:ids',
    target: 'http://localhost:8080/:ids/:ids', 
    options:{},
    events:{}
  },
  {// Rule (3)
    source:'/api/users(/:id)',
    target: 'http://localhost:8080(/:id)', 
    options:{}
  },
  {// Rule (4)
    source:'/api/users(/:id)',
    target: 'http://localhost:8080(/:id)', 
    options:{}
  }
  ...
]

options

{

}

usage

// dependencies
const Koa = require('koa')
const proxy = require('koa-any-proxy')
const app = new Koa()
// middleware
app.use(proxy(rules,options))

notes

请务必在初始化在koa中间件的第一顺位 , 避免不必要问题