vagusX / koa-proxies

a koa@2.x+ proxy middleware
https://vagusx.github.io/koa-proxies/
MIT License
161 stars 44 forks source link

Doesn't support event listening for multiple proxies #65

Closed ljbc1994 closed 2 years ago

ljbc1994 commented 2 years ago

Firstly, thanks for this library, it's got a great api and works well when it's used as a single proxy middleware.

Problem

When registering multiple proxy middleware, the event listeners are only set up for the first proxy but not the subsequent proxies. This should either be supported or there should be a disclaimer on the README to detail that it's not supported. It's more of a bug because the proxy works fine it's just the events that don't get triggered.

The problem is essentially here:

// llne 20
let eventRegistered = false

// line 58
if (events && typeof events === 'object' && !eventRegistered) {

Recreate

function logFirstRequest() {
   console.log('gets called')
}

function logSecondRequest() {
   console.log('doesnt get called')
}

// First middleware
app.use(proxy('/first-test', {
        target: 'http://localhost:5000',
        events: {
            proxyRes: logFirstRequest,
        },
    })
)

// Second middleware
app.use(proxy('second-test', {
        target: 'http://localhost:4999',
        events: {
            proxyRes: logSecondRequest,
        },
    })
)

I've got a fix but will require quite a big change in functionality, if this is something that you'd like fixed?