1.0.11 • Published 8 years ago
kor-proxy v1.0.11
kor-proxy
promise proxy middleware for koa, support Load-Balance
Install
npm install kor-proxyAim
This middleware is supported do two things:
1. Set Proxy Server Four options: (host , protocol , port)
2. 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:pwd@test.url.com: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:pwd@www.proxy.com:8080.It's used for gettingprotocol, auth, host, portproperties, can also be defined explicitly inoptions.(Prefer thanext.rr's element.)Which will pass to http(s).request'soptionsparameters. 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-proxyis based.This method is set,ext.rrwill be ignore(Because the client may be haverrconfiguration).
Error Handle
- When catch Errors, will throw through
ctx.throw(error.message), should deal in your koa app.