express-request-proxy icon indicating copy to clipboard operation
express-request-proxy copied to clipboard

Refactor suggestion: sharing "util" functions among all proxies

Open jamesplease opened this issue 8 years ago • 2 comments

I'm interested in using express-request-proxy to dynamically proxy endpoints. I know this isn't a documented use case, but it's not so hard to do:

const requestProxy = require('express-request-proxy');

function someMiddleware(req, res, next) {
  return requestProxy({ url: dynamicUrl })(req, res, next);
}

The overhead of this extra function call shouldn't be a problem, but I'm a little hesitant to be creating the in-scope functions (such as this one) for each request when that could be avoided.

Would you be open to a PR that would pull those outside of the main export function so that they're shared among all proxies? It would require a slight modification to their signature to accept an options argument. This way, there's less overhead to generating lots and lots (and lots) of proxies.

It'd look something like:

module.exports = function() {
  return function(req, res, next) {
    var options = { ... };
    sharedThing(req, res, next, options);
  }
}

function sharedThing(req, res, next, options) {
 // lala
}

Thanks for reading!

jamesplease avatar Jun 20 '17 22:06 jamesplease

Another option would be to allow url to be a function, so that you could do:

proxy({
  url() {
    return getDaUrl();
  }
})

The motivation behind this request is that I need to include round robin behavior into the proxy.

jamesplease avatar Jun 23 '17 21:06 jamesplease

Apologies @jamesplease, you've probably long since moved on. Think my notification preferences were preventing me from finding out about issues. Would be open to PR however.

dvonlehman avatar May 30 '18 23:05 dvonlehman