Skip to content

Commit

Permalink
Fix bypass proxy middleware (webpack#563)
Browse files Browse the repository at this point in the history
The `bypass` feature was no longer working because the code added the bypass method as middleware AND `httpProxyMiddleware` as middleware. However, if a `bypass` method is given, it should only use that as middleware.

This was caused in webpack#359, and effects `1.15.0` and the 2.x branch.
  • Loading branch information
SpaceK33z committed Aug 23, 2016
1 parent 7790ae9 commit 01700bd
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,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

0 comments on commit 01700bd

Please sign in to comment.