koa-proxy-middleware v2.0.2
koa-proxy-middleware
Middleware for koa2. Reverse proxy middleware for koa. Proxy resources on other servers, such as Java services, and other node.js applications. Based on http-proxy library.
Require
node v8.x +
Installation
Usage
koa-proxy-middleware is used to forward HTTP requests in koa middleware,it looks like the nginx in Node.
example:
const Koa = require('koa');
const Proxy = require('koa-proxy-middleware');
const app = new Koa();
const proxy = new Proxy({
proxies: [
{
host: 'http://localhost:3333/',
context: 'nginx'
},
]
});
app.use(proxy);
app.listen(3000);
API
Options
proxieskoa-proxy-middlewareconfig option,expectArrayvalue,Each of the config objects is a proxy combination,you should fill this option with client requirecontextprefix andhostserver address.hosturl string to be parsed with the url modulecontextLocal proxy root address,required,string formatrewriteunrequired,Function/Booleanvalue. It doesn't overwrite the path when it's false.proxyTimeoutunrequired,Number
proxyTimeout
timeout(in millis) for outgoing proxy requests. unrequired,default 30000rewrite
rewrite the url redirects function.unrequired,default() => path.replace(context, '').It's unnecessary to replace '/' path because funcitonrewritedid it default in http-proxylogLevel
Log level of terminal output,includeserror,warn,info,http,verbose,debug,debug,silly, it dependence on Winston packagehandleReq
The function will be triggered before send data. you can modify the request object of request before handle proxy.This method takes four argumentsproxyReq,req,res,options
const proxy = new Proxy({
proxies: ...,
handleReq: proxyObj => {
{ proxyReq, req, res, options } = proxyObj;
}
});handleRes
The function will be triggered if back response from the target server.It takes three argumentsproxyRes,req,reserror
The error function will be triggered when fail in request to the target server.It takes three argumentserr,req,res
