From 46e05804d8bf03be0700b886a0fba339bed8283a Mon Sep 17 00:00:00 2001 From: Kees Kluskens Date: Tue, 23 Aug 2016 19:49:56 +0200 Subject: [PATCH 1/2] Fix bypass proxy middleware 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 #359, and effects `1.15.0` and the 2.x branch. --- lib/Server.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index c7b993c963..f5a1736fc7 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -169,19 +169,24 @@ function Server(compiler, options) { * ] */ options.proxy.forEach(function(proxyConfig) { + var bypass = typeof proxyConfig.bypass === 'function'; var context = proxyConfig.context || proxyConfig.path; - app.use(function(req, res, next) { - if(typeof proxyConfig.bypass === 'function') { - var bypassUrl = proxyConfig.bypass(req, res, proxyConfig) || false; + function bypassMiddleware(req, res, next) { + var bypassUrl = proxyConfig.bypass(req, res, proxyConfig) || false; - if(bypassUrl) { - req.url = bypassUrl; - } + if(bypassUrl) { + req.url = bypassUrl; } next(); - }, httpProxyMiddleware(context, proxyConfig)); + } + + if(bypass) { + app.use(bypassMiddleware); + } else { + app.use(httpProxyMiddleware(context, proxyConfig)); + } }); } }, From 87ac5ae4c5058ade1084ca139eb1a8e0efcfb7ba Mon Sep 17 00:00:00 2001 From: Kees Kluskens Date: Tue, 23 Aug 2016 21:36:41 +0200 Subject: [PATCH 2/2] Proxy `target` was ignored when using `bypass` method. --- lib/Server.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index f5a1736fc7..a3ba71ad0e 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -171,22 +171,23 @@ 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); + } - function bypassMiddleware(req, res, next) { - var bypassUrl = proxyConfig.bypass(req, res, proxyConfig) || false; + app.use(function(req, res, next) { + var bypassUrl = bypass && proxyConfig.bypass(req, res, proxyConfig) || false; if(bypassUrl) { req.url = bypassUrl; + next(); + } else if(proxyMiddleware) { + return proxyMiddleware(req, res, next); } - - next(); - } - - if(bypass) { - app.use(bypassMiddleware); - } else { - app.use(httpProxyMiddleware(context, proxyConfig)); - } + }); }); } },