Http-proxy-middleware: Dynamically change target via request headers

Created on 7 Jun 2018  路  2Comments  路  Source: chimurai/http-proxy-middleware

Is there any way to dynamically change target depending on request headers?

PS. I tried your router thing, but for it, I still need to predefine urls, however I need them to be fully dynamic.

Most helpful comment

You should still be able to define fully dynamic targets with the router, as it accepts the request, from which you can grab the headers. So, something like this:

app.use(proxy({
  target: 'http://example.com',
  router (req) => {
    let newTarget = req.headers['my-header-containing-the-url-or-flag']
    // ... transformation logic ... //
    return newTarget
  }
})

All 2 comments

You should still be able to define fully dynamic targets with the router, as it accepts the request, from which you can grab the headers. So, something like this:

app.use(proxy({
  target: 'http://example.com',
  router (req) => {
    let newTarget = req.headers['my-header-containing-the-url-or-flag']
    // ... transformation logic ... //
    return newTarget
  }
})

@UseHandsfree Thanks for contribution, that works well!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lukaszpochrzest picture lukaszpochrzest  路  3Comments

udayms picture udayms  路  8Comments

kimmobrunfeldt picture kimmobrunfeldt  路  5Comments

dzz9143 picture dzz9143  路  3Comments

mshindal picture mshindal  路  4Comments