Skip to content

Commit 7285c05

Browse files
committed
Add option to bypass proxying requests based on a function call
The primary use-case behind this addition is to allow HTML5 history support for proxied paths. E.g.: ``` proxy: { '/thepath*': { target: 'https://otherserver.example.com', bypass: function(req) { if (req.headers.accept.indexOf('html') !== -1) { console.log('Skipping proxy for browser request.'); return '/index.html'; } }, }, }, ```
1 parent a8e4ab8 commit 7285c05

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

lib/Server.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,25 @@ function Server(compiler, options) {
134134
}
135135
options.proxy.forEach(function (proxyOptions) {
136136
proxyOptions.ws = proxyOptions.hasOwnProperty('ws') ? proxyOptions.ws : true;
137-
app.all(proxyOptions.path, function (req, res) {
138-
if(typeof proxyOptions.rewrite === 'function') proxyOptions.rewrite(req, proxyOptions);
139-
if (proxyOptions.host) {
140-
req.headers.host = proxyOptions.host;
141-
}
142-
proxy.web(req, res, proxyOptions, function(err){
143-
var msg = "cannot proxy to " + proxyOptions.target + " (" + err.message + ")";
144-
this.io.sockets.emit("proxy-error", [msg]);
145-
res.statusCode = 502;
146-
res.end();
147-
}.bind(this));
148-
if (proxyOptions.configure) {
149-
proxyOptions.configure(proxy);
137+
app.all(proxyOptions.path, function (req, res, next) {
138+
var bypassUrl = typeof proxyOptions.bypass === 'function' ? proxyOptions.bypass(req, res, proxyOptions) : false;
139+
if (bypassUrl) {
140+
req.url = bypassUrl;
141+
next();
142+
} else {
143+
if(typeof proxyOptions.rewrite === 'function') proxyOptions.rewrite(req, proxyOptions);
144+
if (proxyOptions.host) {
145+
req.headers.host = proxyOptions.host;
146+
}
147+
proxy.web(req, res, proxyOptions, function(err){
148+
var msg = "cannot proxy to " + proxyOptions.target + " (" + err.message + ")";
149+
this.io.sockets.emit("proxy-error", [msg]);
150+
res.statusCode = 502;
151+
res.end();
152+
}.bind(this));
153+
if (proxyOptions.configure) {
154+
proxyOptions.configure(proxy);
155+
}
150156
}
151157
}.bind(this));
152158
}.bind(this));

0 commit comments

Comments
 (0)