Http-proxy-middleware: option.router with callback in arguments

Created on 17 Mar 2017  路  13Comments  路  Source: chimurai/http-proxy-middleware

Hi, thanks for awesome library. I'm trying to use it as proxy for app but I have to do async actions to resolve new target.

It would be great to have possibility to do something like this:

var apiProxy = proxy('/api', {
  target: 'http://localhost:1234',
  router: function (req, callback) {
     myAsyncApi()
          .then(function(newTarget){
                 callback('newTarget');
          })
  }
});
enhancement need votes

Most helpful comment

Thank you for the kind words.

Never thought of the be used this way.

Will consider adding it if more users have to need for this.

All 13 comments

I can achieve my goal by adding middleware before proxy to do that asynchronous actions before and keep 'newTarget' in req object. But still I would be nice to have 'callback' in arguments.

Thank you for the kind words.

Never thought of the be used this way.

Will consider adding it if more users have to need for this.

Hi!
I need this async option too!
In my case I have a files on different servers and need to resolve target from DB by fileid,

Hi, I also need to generate dynamic url after execution of some sync method, because my url needed some Ids which i will have to fetch from server.
Can you add this callback?

Yes please!

I can achieve my goal by adding middleware before proxy to do that asynchronous actions before and keep 'newTarget' in req object.

Could you please enlighten me on the idea of adding a middleware before proxy? Did you mean to create a separate webpack dev server middleware?

@matrunchyk
I believe the idea is to do async actions to resolve new target within the middleware before proxy and inject it in the req object, something like

this.expressApp.use(
    '/request',
    async (req, _res, next) => {
        req.newTarget = await yourAsyncAction();
        next();
    },
    proxy({
        //...
        router: req => req.newTarget
    })
);

@chimurai Would love to see this feature! Happy to create PR if you like this idea.

I can achieve my goal by adding middleware before proxy to do that asynchronous actions before and keep 'newTarget' in req object. But still I would be nice to have 'callback' in arguments.

It was a long time ago now, but can you share your code ?
I'm trying to do something similar but with no success...
I'm trying to route based on the JWT token.

@trash-anger
It was long time ago. See example two posts above: https://github.com/chimurai/http-proxy-middleware/issues/153#issuecomment-516494566

If you need further help paste some code.

+1
The solution with middleware would not work for websockets, as the middleware would not get triggered

published: [email protected] with async router support

The solution with middleware would not work for websockets, as the middleware would not get triggered

I'm not sure about that but it could work if you create specific endpoint for each websocket stream ! ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

noahgrant picture noahgrant  路  4Comments

SidneyNemzer picture SidneyNemzer  路  5Comments

DylanPiercey picture DylanPiercey  路  6Comments

folterung picture folterung  路  4Comments

zh-h picture zh-h  路  3Comments