dvonlehman / express-request-proxy

Advanced streaming http request proxy middleware for Express with support for custom routes, caching, and response transforms.
95 stars 38 forks source link

Refactor suggestion: sharing "util" functions among all proxies #27

Open jamesplease opened 7 years ago

jamesplease commented 7 years ago

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 commented 7 years ago

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.

dvonlehman commented 6 years ago

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.