Comments (9)
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.
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.
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¶ms.
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.
Could you share your pathRewrite
config object and the request paths before and after rewrite? (Expected path after rewrite)
from http-proxy-middleware.
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.
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.
#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.
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.
@Lalem001 I copied your question to keep the discussions separate.
from http-proxy-middleware.
Related Issues (20)
- Filter example in README wrong. HOT 1
- Cannot override options in the `onProxyReq` method. HOT 1
- websocket messages interception & manipulation
- Intercept HTTP(s) calls and modify request "paths" on the fly HOT 1
- Issue obtaining body and updating it as part of proxy HOT 3
- Proxy request fails due to bodyParser HOT 1
- Fails to proxy from localhost to localhost HOT 2
- Too many subscriptions to Server.close event cause a OOM? HOT 4
- TS type compatibility HOT 4
- ERR_HTTP_HEADERS_SENT from onProxyReq with https-proxy-agent HOT 2
- Proxy to another proxy HOT 1
- Security Vulnerability in Dependency | follow-redirects HOT 2
- "Invalid context" error message is unhelpful when mixing paths with globs HOT 1
- Release plan for v3.0.0 HOT 1
- Response interceptor copyHeaders logic only removes first character of cookie domain because of non-greedy regex
- TypeError: (options.buffer || req).pipe is not a function HOT 1
- Allow custom Http Method like LIST HOT 3
- SSE close connection issue with proxy HOT 3
- V3: base path not stripped from WebSocket upgrade requests HOT 2
- SSL Error in when Upgrading from node 16 to 18 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from http-proxy-middleware.