kor-proxy
v1.0.11
Published
promise proxy middleware for koa
Downloads
4
Readme
kor-proxy
promise proxy middleware for koa, support Load-Balance
Install
npm install kor-proxy
Aim
This middleware is supported do two things:
- Set Proxy Server Four options: (host [, protocol] [, auth] [, port])
- Rewrite the proxy request Header
Match request will respond automatical internally use
Stream.pipe
.
Hello Kor-proxy
const Koa = require('neat-kor'); // router wrap for Koa
const proxy = require('kor-proxy');
const app = new Koa();
const ext = {
timeout: 1000,
headerRewrite() {
// selected, deal req.headers before proxy;
},
dealTimeout() {
// if none, will throw ('proxy-timeout');
},
rr: [options1, options2, ...] // every time merge one element to options
}
app.get('/proxy1', proxy('https://auth:[email protected]:8080', ext));
// the same as http(s).request 's options parameter
const options = {
protocol: 'https', // defalut is http:
auth: 'auth:pwd', // default is null
host: 'test.url.com', // must pass!
port: 8088, // defalut is 80(443)
};
app.get('/proxy2', proxy(options, ext));
Load-Balance
const Koa = require('neat-kor'); // router wrap for Koa
const proxy = require('kor-proxy');
const app = new Koa();
const rr = [{
host: 'target.url.com1',
port: 8000
},
{
host: 'target.url.com2',
port: 9000
},
];
const ext = {
timeout: 1000,
headerRewrite() {
// selected, deal req.headers before proxy;
},
dealTimeout() {
// if none, will throw ('proxy-timeout');
},
rr// every time merge one element to options, except the same key.
}
// the same as http(s).request 's options parameter
const options = {
port: 8088, // defalut is 80(443)
};
// Important: In this example, proxy server port will always be 8088, because options is prefer than rr's element.
app.get('/proxy2', proxy(options, ext));
API
proxy(options [, ext])
options
(str | obj) - Default is{}
.The absolute url path for proxy target, Eg:http(s)://auth:[email protected]:8080
.It's used for gettingprotocol, auth, host, port
properties, can also be defined explicitly inoptions
.(Prefer thanext.rr
's element.)Which will pass to http(s).request'soptions
parameters. Eg:agent, headers..
ext
(obj) - Default is{}
.extension object.ext.rr
(Array) - : Default isundefined
.Every element of arr will merge intooptions
, in a round-robin manner. especially when need Load-Balance. (Ifrr
's element have same key withoptions
, it will not merge into, please put common key in options, dynamic for Load-Banlance put inrr
)ext.timeout
(num) - Defalut is15s
, timeout(ms) between proxy request send and recieve response.ext.headerRewrite
(fn) - : Default isundefined
, deal headers before proxy, recieve one param, the rawmessage.headers
.ext.dealTimeout
(fn) - : Default isundefined
, deal timeout error when proxy, if none willctx.throw('proxy-timeout')
.ext.client
(fn) - : Default isundefined
, Custom client, can created byneat-http
, whichkor-proxy
is based.This method is set,ext.rr
will be ignore(Because the client may be haverr
configuration).
Error Handle
- When catch Errors, will throw through
ctx.throw(error.message)
, should deal in your koa app.