Skip to content
17 changes: 14 additions & 3 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,23 @@ class Server {
}

const bypass = typeof proxyConfig.bypass === 'function';
Comment thread
mistic marked this conversation as resolved.
Outdated

const bypassRawValue = bypass
? proxyConfig.bypass(req, res, proxyConfig)
: null;
const bypassUrl =
(bypass && proxyConfig.bypass(req, res, proxyConfig)) || false;
typeof bypassRawValue === 'string'
? bypassRawValue
: typeof bypassRawValue === 'boolean' &&
bypassRawValue === false;
Comment thread
mistic marked this conversation as resolved.
Outdated

if (bypassUrl) {
Comment thread
mistic marked this conversation as resolved.
Outdated
req.url = bypassUrl;
if (typeof bypassUrl === 'boolean') {
req.url = null;
}

if (typeof bypassUrl === 'string') {
req.url = bypassUrl;
}

next();
} else if (proxyMiddleware) {
Expand Down
13 changes: 13 additions & 0 deletions test/Proxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ const proxyOptionPathsAsProperties = {
if (/\.html$/.test(req.path)) {
return '/index.html';
}

return null;
},
},
'/proxyfalse': {
bypass(req) {
if (/\/proxyfalse$/.test(req.path)) {
return false;
}
},
},
};
Expand Down Expand Up @@ -116,6 +125,10 @@ describe('Proxy', () => {
it('should pass through a proxy when a bypass function returns null', (done) => {
req.get('/foo.js').expect(200, /Hey/, done);
});

it('should not pass through a proxy when a bypass function returns false', (done) => {
req.get('/proxyfalse').expect(404, done);
});
});
});

Expand Down