Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

koa-proxy

edorivai89.2kMIT1.0.0-alpha.3TypeScript support: definitely-typed

Proxy middleware for koa

koa, middleware, proxy

readme

koa-proxy Build Status

Proxy middleware for koa


Install

$ npm install koa-proxy -S

Usage

When you request http://localhost:3000/index.js, it will fetch http://alicdn.com/index.js and return.

var koa = require('koa');
var proxy = require('koa-proxy');
var app = koa();
app.use(proxy({
  host: 'http://alicdn.com'
}));
app.listen(3000);

You can proxy a specified url.

app.get('index.js', proxy({
  url: 'http://alicdn.com/index.js'
}));

You can specify a key/value object that can map your request's path to the other.

app.get('index.js', proxy({
  host: 'http://alicdn.com',
  map: {
    'index.js': 'index-1.js'
  }
}));

You can specify a function that can map your request's path to the desired destination.

app.get('index.js', proxy({
  host: 'http://alicdn.com',
  map: function(path) { return 'public/' + path; }
}));

You can specify match criteria to restrict proxy calls to a given path.

app.use(proxy({
  host:  'http://alicdn.com', // proxy alicdn.com...
  match: /^\/static\//        // ...just the /static folder
}));

Or you can use match to exclude a specific path.

app.use(proxy({
  host:  'http://alicdn.com',     // proxy alicdn.com...
  match: /^(?!\/dontproxy\.html)/ // ...everything except /dontproxy.html
}));

Proxy won't send cookie to real server, you can set jar = true to send it.

app.use(proxy({
  jar: true,
}));

Proxy won't send 'foo' and 'bar' headers to real server, or recieve 'jar-jar' from real server.

app.use(proxy({
  suppressRequestHeaders: ['foo','bar'], // case-insensitive
  suppressResponseHeaders: ['jar-jar'] // case-insensitive
}));

You can also add new headers to your response or override existing ones

app.use(proxy({
  overrideResponseHeaders: {
    "cow": "moo",
    "duck": "quack"
    }, 
}));

LICENSE

Copyright (c) 2014 popomore. Licensed under the MIT license.

changelog

0.9.0 / 2017-09-04

  • Added suppressRequestHeaders and suppressResponseHeaders options (#54)

0.8.0 / 2017-02-03

  • feat: [BREAKING_CHANGE] set jar = false by default (#48)
  • feat: add a yieldNext option (#47)

0.7.0 / 2016-12-27

  • Add option to enable/disable following redirects (#45)
  • surely most people would want to install this as regular dependency, not as global one (#36)

0.6.0 / 2016-04-28

  • Support requestOptions as functions (#28)

0.5.0 / 2016-02-24

  • Exposing the cookie jar to the options interface
  • fix: should not return Transfer-encoding header
  • Added support for requestOptions to be passed as options to the underlying request
  • strip trailing slash from headers.host

0.4.1 / 2015-10-09

  • check contentType is defined before calling indexOf (fixes bug when used in conjunction with koa-bodyparser)

0.4.0 / 2015-09-09

  • added ability to have map as a function rather than an object

0.3.0

Add host and match option

0.2.0

Munge body object to JSON when appropriate

0.1.3

  • fix headers option
  • switch to co-request
  • pass request method and body to proxy

0.1.2

return proxy's statusCode

0.1.1

should pass querystring

0.1.0

First commit