koa-proxies

Http Proxy Middleware for Koa@2.x/next

Please upgrade this package to 0.6+
Last updated 7 years ago by vagusx .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install koa-proxies 
SYNC missed versions from official npm registry.

Koa Proxies

NPM

Node.js CI Coverage NPM Downloads Greenkeeper badge

Koa@2.x/next middlware for http proxy

Powered by http-proxy.

Installation

$ npm install koa-proxies --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/',
  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
}))

The 2nd parameter options can be a function. It will be called with the path matching result (see path-match for details) and Koa ctx object. You can leverage this feature to dynamically set proxy. Here is an example:

// dependencies
const Koa = require('koa')
const proxy = require('koa-proxies')

const app = new Koa()

// middleware
app.use(proxy('/octocat/:name', (params, ctx) => {
  return {
    target: 'https://api.github.com/',
    changeOrigin: true,
    rewrite: () => `/users/${params.name}`,
    logs: true
  }})
)

Moreover, if the options function return false, then the proxy will be bypassed. This allows the middleware to bail out even if path matching succeeds, which could be helpful if you need complex logic to determine whether to proxy or not.

Attention

Please make sure that koa-proxies is in front of koa-bodyparser to avoid this issue 55

const Koa = require('koa')
const app = new Koa()
const proxy = require('koa-proxies')
const bodyParser = require('koa-bodyparser')

app.use(proxy('/user', {
  target: 'http://example.com',
  changeOrigin: true
}))

app.use(bodyParser())

JavaScript Style Guide

Current Tags

  • 0.12.4                                ...           latest (a year ago)

20 Versions

  • 0.12.4                                ...           a year ago
  • 0.12.3                                ...           2 years ago
  • 0.12.2                                ...           3 years ago
  • 0.12.1                                ...           4 years ago
  • 0.11.0                                ...           5 years ago
  • 0.10.0                                ...           5 years ago
  • 0.9.0                                ...           5 years ago
  • 0.8.1                                ...           6 years ago
  • 0.8.0                                ...           6 years ago
  • 0.7.0                                ...           7 years ago
  • 0.6.2                                ...           7 years ago
  • 0.6.1                                ...           7 years ago
  • 0.6.0                                ...           7 years ago
  • 0.5.2 [deprecated]           ...           7 years ago
  • 0.5.1 [deprecated]           ...           7 years ago
  • 0.5.0 [deprecated]           ...           7 years ago
  • 0.4.0 [deprecated]           ...           7 years ago
  • 0.3.0 [deprecated]           ...           8 years ago
  • 0.2.0 [deprecated]           ...           8 years ago
  • 0.1.0 [deprecated]           ...           8 years ago
Maintainers (1)
Downloads
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (3)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |