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

Fix bypass proxy middleware #563

Merged
merged 2 commits into from
Aug 23, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,25 @@ function Server(compiler, options) {
* ]
*/
options.proxy.forEach(function(proxyConfig) {
var bypass = typeof proxyConfig.bypass === 'function';
var context = proxyConfig.context || proxyConfig.path;
var proxyMiddleware;
// It is possible to use the `bypass` method without a `target`.
// However, the proxy middleware has no use in this case, and will fail to instantiate.
if(proxyConfig.target) {
proxyMiddleware = httpProxyMiddleware(context, proxyConfig);
}

app.use(function(req, res, next) {
if(typeof proxyConfig.bypass === 'function') {
var bypassUrl = proxyConfig.bypass(req, res, proxyConfig) || false;
var bypassUrl = bypass && proxyConfig.bypass(req, res, proxyConfig) || false;

if(bypassUrl) {
req.url = bypassUrl;
}
if(bypassUrl) {
req.url = bypassUrl;
next();
} else if(proxyMiddleware) {
return proxyMiddleware(req, res, next);
}

next();
}, httpProxyMiddleware(context, proxyConfig));
});
});
}
},
Expand Down