Skip to content

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

Notifications You must be signed in to change notification settings

junyiz/koa-proxy-pass

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koa-proxy-pass build status

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'
  match: /\//
}));
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

'match' is required

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-2023 junyiz. Licensed under the MIT license.

About

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published