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
-
proxies
koa-proxy-middleware
config option,expectArray
value,Each of the config objects is a proxy combination,you should fill this option with client requirecontext
prefix andhost
server address.-
host
url string to be parsed with the url module -
context
Local proxy root address,required,string format -
rewrite
unrequired,Function
/Boolean
value. It doesn't overwrite the path when it's false. -
proxyTimeout
unrequired,Number
-
-
proxyTimeout
timeout(in millis) for outgoing proxy requests. unrequired,default 30000 -
rewrite
rewrite the url redirects function.unrequired,default() => path.replace(context, '')
.It's unnecessary to replace '/' path because funcitonrewrite
did it default in http-proxy -
logLevel
Log level of terminal output,includeserror
,warn
,info
,http
,verbose
,debug
,debug
,silly
, it dependence on Winston package -
handleReq
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
,res
-
error
The error function will be triggered when fail in request to the target server.It takes three argumentserr
,req
,res