UNPKG

koa2-proxy-middleware

Version:

a koa2 proxy middleware by means of http-proxy-middleware

18 lines (16 loc) 476 B
const httpProxy = require('http-proxy-middleware'); const k2c = require('koa2-connect'); const pathToRegexp = require('path-to-regexp'); module.exports = (options) => { return async function (ctx, next) { const { targets = {} } = options; const { path } = ctx; for (const route of Object.keys(targets)) { if (pathToRegexp(route).test(path)) { await k2c(httpProxy(targets[route]))(ctx, next); break; } } await next(); }; };