You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// enable logoptions.logs=true;// or false// custom log functionoptions.logs=(ctx,target){console.log('%s - %s %s proxy to -> %s',newDate().toISOString(),ctx.req.method,ctx.req.oldPath,newURL(ctx.req.url,target))}
Usage
// dependenciesconstKoa=require('koa')constproxy=require('koa-proxies')consthttpsProxyAgent=require('https-proxy-agent')constapp=newKoa()// middlewareapp.use(proxy('/octocat',{target: 'https://api.github.com/users/',changeOrigin: true,agent: newhttpsProxyAgent('https://1.2.3.4:88'),// if you need or just delete this linerewrite: path=>path.replace(/^\/octocat(\/|\/\w+)?$/,'/vagusx'),logs: true}))
The 2nd parameter options can be a function. It will be called with the path matching result (see path-match for details) and Koa ctx object. You can leverage this feature to dynamically set proxy. Here is an example:
Moreover, if the options function return false, then the proxy will be bypassed. This allows the middleware to bail out even if path matching succeeds, which could be helpful if you need complex logic to determine whether to proxy or not.
Attention
Please make sure that koa-proxies is in front of koa-bodyparser to avoid this issue 55