var proxy = require('koa-http2-proxy2');var apiProxy = proxy('/api', { target: 'http://www.example.org' });// \____/ \_____________________________/// | |// context options// 'apiProxy' is now ready to be used as middleware in a server.
context: Determine which requests should be proxied to the target host.
(more on context matching)
options.target: target host to proxy to. (protocol + host)
Providing an alternative way to decide which requests should be proxied; In case you are not able to use the server's path parameter to mount the proxy or when you need more flexibility.
proxy({...}) - matches any path, all requests will be proxied.
proxy('/', {...}) - matches any path, all requests will be proxied.
proxy('/api', {...}) - matches paths starting with /api
proxy(/^\/([a-zA-Z0-9_/-]{1,})$/, {...}) - matches paths with regexp
multiple path matching
proxy(['/api', '/ajax', '/someotherpath'], {...})
wildcard path matching
For fine-grained control you can use wildcard matching. Glob pattern matching is done by micromatch. Visit micromatch or glob for more globbing examples.
proxy('**', {...}) matches any path, all requests will be proxied.
proxy('**/*.html', {...}) matches any path which ends with .html
Options
option.pathRewrite: object/function, rewrite target's url path. Object-keys will be used as RegExp to match paths.
option.router: object/function, re-target option.target for specific requests.
// Use `host` and/or `path` to match requests. First match will be used.// The order of the configuration matters.
Shorthand
Use the shorthand syntax when verbose configuration is not needed. The context and option.target will be automatically configured when shorthand is used. Options can still be used if needed.
In the previous WebSocket examples, http-proxy-middleware relies on a initial http request in order to listen to the http upgrade event. If you need to proxy WebSockets without the initial http request, you can subscribe to the server's http upgrade event manually.
var wsProxy = proxy('ws://echo.websocket.org');var app = new Koa();app.use(wsProxy);var server = app.listen(3000);server.on('upgrade', wsProxy.upgrade); // <-- subscribe to http 'upgrade'
Tests
Run the test suite:
# install dependencies$ yarn# linting$ yarn lint$ yarn lint:fix# building (compile typescript to js)$ yarn build# unit tests$ yarn test# code coverage$ yarn cover
Copyright for portions of this project are held by Steven Chim, 2015-2019 as part of http-proxy-middleware. All other copyright for this project are held by Ontola BV, 2019.
// verbose replacementfunction logProvider(provider) { var logger = new (require('winston')).Logger(); var myCustomProvider = { log: logger.log, debug: logger.debug, info: logger.info, warn: logger.warn, error: logger.error }; return myCustomProvider;}
option.onError: function, subscribe to http-proxy's error event for custom error handling.
function onError(err, ctx) { ctx.response.status = 500; ctx.response.body = 'Something went wrong. And we are reporting a custom error message.';}
option.onProxyRes: function, subscribe to http-proxy's proxyRes event.
function onProxyRes(proxyRes, ctx) { proxyRes.headers['x-added'] = 'foobar'; // add new header to response delete proxyRes.headers['x-removed']; // remove header from response}
option.onProxyReq: function, subscribe to http-proxy's proxyReq event.
function onProxyReq(proxyReq, ctx) { // add custom header to request proxyReq.setHeader('x-added', 'foobar'); // or log the req}
option.onUpgrade: function, called before upgrading a websocket connection.
onUpgrade: async ctx => { // add session middleware to the websocket connection // see option.app await session(ctx, () => {});};
option.app: koa app, used to generate a koa ctx to be used in onUpgrade. If left blank, a object containing only req will be used as context