koa-proxies-router
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Koa Proxies with router config - Forked from koa-proxies and rewrited!

NPM TavisCI Build CircieCI Build Coverage NPM Downloads Greenkeeper badge

Koa@2.x/next middlware for http proxy

Add router config like http-proxy-middleware Powered by http-proxies / http-proxy-middleware

Installation

$ npm install koa-proxies-router --save

Options

http-proxy events

options.events = {
  error(err, req, res) {},
  proxyReq(proxyReq, req, res) {},
  proxyRes(proxyRes, req, res) {}
}

log option

// enable log
options.logs = true; // or false
 
// custom log function
options.logs = (ctx, target) {
  console.log('%s - %s %s proxy to -> %s', new Date().toISOString(), ctx.req.method, ctx.req.oldPath, new URL(ctx.req.url, target))
}

Usage

// dependencies
const Koa = require('koa')
const proxy = require('koa-proxies')
const httpsProxyAgent = require('https-proxy-agent')
 
const app = new Koa()
 
// middleware
app.use(proxy('/octocat', {
  target: 'https://api.github.com/users', // if router config exist this will be ignored
  router: (req) {
    if (req.query._proxy) {
      return _proxy
    }
    return 'https://api.github.com/users',
  },
  changeOrigin: true,
  agent: new httpsProxyAgent('http://1.2.3.4:88'), // if you need or just delete this line
  rewrite: path => path.replace(/^\/octocat(\/|\/\w+)?$/, '/vagusx'),
  logs: true
}))

JavaScript Style Guide

Package Sidebar

Install

npm i koa-proxies-router

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

330 kB

Total Files

10

Last publish

Collaborators

  • luxueyan