You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
808 B
24 lines
808 B
1 month ago
|
const k2c = require('koa2-connect')
|
||
|
const httpProxy = require('http-proxy-middleware')
|
||
|
const contextMatcher = require('http-proxy-middleware/dist/context-matcher.js')
|
||
|
|
||
|
module.exports = (options = {}) => ({ _root, app }) => {
|
||
|
const proxies = Object.entries(options).reduce(
|
||
|
(proxies, [context, contextOptions]) =>
|
||
|
proxies.set(context, httpProxy.createProxyMiddleware(context, contextOptions)),
|
||
|
new Map()
|
||
|
)
|
||
|
|
||
|
return app.use(async (ctx, next) => {
|
||
|
for (const context of proxies.keys()) {
|
||
|
// emulate `shouldProxy` from http-proxy-middleware
|
||
|
// because otherwise we might call next() more than once and break koa
|
||
|
if (contextMatcher.match(context, ctx.path, ctx.req)) {
|
||
|
await k2c(proxies.get(context))(ctx, next)
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
await next()
|
||
|
})
|
||
|
}
|