-
Notifications
You must be signed in to change notification settings - Fork 2k
Fix proxy path #645
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
Fix proxy path #645
Conversation
This fix considers the actual target path again (which has been ignored).
@Domiii I can see this being valid but you need to handle the case where path is not defined. See the failing tests under |
Yeah. I suppose, we should also use |
@Domiii well it needs to be ignored in the case where the path is |
I would instead like to see a hook so that is could be fixed manually. |
I ran into a similar issue when using this in my project. Having the full path honored seems to be the expected functionality so hopefully this will be patch in a future release. For the time being, here is an example work around that doesn't require any updates to var httpProxy = require('http-proxy');
// Additional dependencies needed
var path = require('path');
var url = require('url');
var proxy = httpProxy.createProxy();
var routes = {
'foo.com': 'http://website.com:8001',
'bar.com': 'http://website2.com:8002/foo',
'baz.com': 'http://website.com:8001/foo/bar',
'qux.com': 'http://website2.com:8002/foo/bar/baz'
};
require('http').createServer(function(req, res) {
// Before sending to the proxy, I actually test to see
// there is a key present in the proxy table (aka, routes)
if (routes[req.headers.host]) {
// parse out the hostname
var route = url.parse(routes[req.headers.host]);
// use path to neatly join the route path to the
// requested url. this keeps the output valid when
// the route.path == '/'
req.url = path.join(route.path, req.url);
// send off to the proxy as you normally would
return proxy.web(req, res, {
target: routes[req.headers.host]
});
}
// If we have no proxy target just send a 404
res.writeHead(404);
res.end();
}).listen(8000); |
@greaterweb thanks for the example! I just modified your comment to have the correct api :). |
@@ -57,10 +57,14 @@ common.setupOutgoing = function(outgoing, options, req, forward) { | |||
) { outgoing.headers.connection = 'close'; } | |||
} | |||
|
|||
|
|||
// the final path is target path + relative path requested by user: | |||
outgoing.path = options.target.path; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this better fit as written like below? This matches the convention used throughout the setupOutgoing
method.
outgoing.path = options[forward || 'target'].path;
This could also be expanded to be written as below. This would likely satisfy the integration testing.
outgoing.path = options[forward || 'target'].path || '';
If we don't like the idea of setting outgoing.path
as an empty string this could easily be rewritten to be wrapped in some conditionals.
Thanks for the fix, @jcrugzz Copy and paste fail on my end :-) I coupled things from the ProxyTable example and my project (which uses express and does a few other things). |
What's the next step for this? Just fix the tests? |
@EndangeredMassa yes it needs to be able to pass the current tests and there should be tests for the use case that this is a fix for. |
@Domiii do you plan on fixing up the tests for this? If not, I could take a look. |
@EndangeredMassa that would be huge. |
Submitted a new PR (that includes this original commit) to address the tests. Required some code changes. |
Fixed in #693 |
Thank you, EndangeredMassa! And sorry for the late response! |
No worries! |
This fix considers the actual target path again (which has been ignored).