Coder Social home page Coder Social logo

Comments (9)

chimurai avatar chimurai commented on May 17, 2024

Interesting points...

Think _.forIn would be a better choice for object iteration.

Stopping at first match or cycle through everything are both confusing; Since iteration order is not guaranteed (_.forEach and _.forIn)

So looping through everything wouldn't improve the situation.

The majority in open source projects are using simple rewrite-rules to rewrite paths; They probably won't face any issues.

I'll update the documentation explaining the rewrite behavior (stopping at first match and order not guaranteed).


Perhaps allowing you to provide a custom function to do your own rewrite logic makes more sense to handle more complicated use-cases.

from http-proxy-middleware.

chimurai avatar chimurai commented on May 17, 2024

I've no idea how pathRewrite is being used in private/closed projects. But if there is need for more control to handle more complicated path rewrites, I would expect a feature request in the issue tracker.

Keeping things simple and update the docs to remove the confusion would be my preference for now. What do you think @brandonculver ?

from http-proxy-middleware.

brandonculver avatar brandonculver commented on May 17, 2024

I agree. It would potentially be a breaking change for people already using it. Personally, I just removed the return false locally and it works as I expected in my head. Maybe there is a better solution for my use case. I am removing the original path, and stripping out an auth token (query parameter). Without doing that the proxied url ended up being /target/api/incoming/api?authToken&more&necessary&params.

The auth token may, or may not be directly after the original api in the string or the regex would be simple. There is probably a way to say match original api string and auth token, replace with ' ' but my regex fu is lacking.

from http-proxy-middleware.

chimurai avatar chimurai commented on May 17, 2024

Could you share your pathRewrite config object and the request paths before and after rewrite? (Expected path after rewrite)

from http-proxy-middleware.

brandonculver avatar brandonculver commented on May 17, 2024

I really can't unfortunately do to some internal agreements. The pseudo code I posted is accurate though.

client calls /original/api/call?queryParam=abc&token=123

but token may or may not be the first,second,third, etc parameter

proxied to /a/totally/different/api?queryParam=abc

we strip off the the old api path and the token.

my config is simple

{
  target: 'http://host/a/totally/different/api',
  pathRewrite: {
    "/original/api/call": '',
    "[\?,\&]token=([^\&]+)": ''
  }
}

By removing return false, it hits both of those rewrites and strips out the original api base path and the token.

from http-proxy-middleware.

chimurai avatar chimurai commented on May 17, 2024

Understandable. Thanks for sharing the pseudo code.

Another reason for exiting the loop early, was from performance point of view.
With each request all pathRewrite rules would be checked if rewrite can/should be applied; This matches your expectation ;-) (I never thought of applying multiple rewrite rules on 1 path...)

Think changing the rewrite behaviour wouldn't hurt too much by removing the early iteration exit restriction, since I haven't seen extensive rewrite configurations in the wild. (Even though it might break existing use cases)

I'll have to think about this change...

from http-proxy-middleware.

chimurai avatar chimurai commented on May 17, 2024

#73 should give you the possibility to provide a custom path rewrite function. It will be backwards compatible.

#72 should remove limitation of 1 rewrite. (Scheduled for the v1.0.0 release, since it might break some use cases)

Think this should cover a lot of use-cases.

@brandonculver Hopefully these changes will cover you use-cases.

from http-proxy-middleware.

Lalem001 avatar Lalem001 commented on May 17, 2024

Would picking up a syntax like in http-rewrite-middleware work?

Example from their docs:

var rewriteMiddleware = rewriteModule.getMiddleware([
    // Internal rewrite 
    {from: '^/index_dev.html$', to: '/src/index.html'},
    // Internal rewrite 
    {from: '^/js/(.*)$', to: '/src/js/$1'},
    // 301 Redirect 
    {from: '^/old-stuff/(.*)$', to: '/new-cool-stuff/$1', redirect: 'permanent'},
    // 302 Redirect 
    {from: '^/stuff/(.*)$', to: '/temporary-stuff/$1', redirect: 'temporary'}
]);

Having the paths in an array like this would guarantee order, and you can apply each rule in succession.

Thoughts?

P.S. I am actually using http-rewrite-middleware in my own projects, but it interferes with the operation of http-proxy-middleware@>=0.14.0 somehow.

from http-proxy-middleware.

chimurai avatar chimurai commented on May 17, 2024

@Lalem001 I copied your question to keep the discussions separate.

from http-proxy-middleware.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.