@junyiz/koa-proxy-pass

A light-weight, no-dependency Proxy middleware for koajs, it's similar to nginx's proxy_pass

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
20Mar 7, 2021Jul 27, 2018Minified + gzip package size for @junyiz/koa-proxy-pass in KB

Readme

Build Status

koa-proxy-pass

A light-weight, no-dependency Proxy middleware for koajs, it's similar to nginx's proxy_pass.

Install

$ npm install @junyiz/koa-proxy-pass -S

Usage

When you request http://localhost:8090, it will fetch https://github.com and return.

const Koa = require('koa');
const proxyPass = require('koa-proxy-pass');
const app = new Koa();
app.use(proxyPass({
  host: 'https://github.com'
}));
app.listen(8090);

You can proxy a specified path, use RegExp.

const Koa = require('koa');
const proxyPass = require('./koa-proxy-pass');
const app = new Koa();
app.use(proxyPass({
  host: 'https://github.com',
  match: /^\/junyiz/
}));
app.listen(8090);

You can proxy a specified path, use koa-router.

const Koa = require('koa');
const Router = require('koa-router');
const proxyPass = require('./koa-proxy-pass');
const app = new Koa();
const router = new Router();
router.get('/junyiz', proxyPass({
  host: 'https://github.com'
}));
app.use(router.routes())
app.use(router.allowedMethods());
app.listen(8090);

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

const Koa = require('koa');
const proxyPass = require('./koa-proxy-pass');
const app = new Koa();
app.use(proxyPass({
  host: 'https://github.com',
  map: {
    '/hint.svg': '/images/search-shortcut-hint.svg'
  }
}));
app.listen(8090);

Options

host

Type: String

'host' is required

match

Type: RegExp

map

Type: Object or Function

Object is a path mapping of key-value, Function that handles paths

headers

Type: Object

This allows you to override request headers

LICENSE

Copyright (c) 2018-2019 junyiz. Licensed under the MIT license.

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.