Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jamesplease opened this issue Jun 20, 2017 · 2 comments
Open

Comments

@jamesplease
Copy link

jamesplease commented Jun 20, 2017

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
Copy link
Author

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
Copy link
Owner

dvonlehman commented May 30, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants