Skip to content

Commit efd32a0

Browse files
committed
Change to use http-proxy-middleware instead of http-proxy. Keeps bypass functionality.
1 parent 4b58b42 commit efd32a0

File tree

2 files changed

+60
-34
lines changed

2 files changed

+60
-34
lines changed

Diff for: lib/Server.js

+58-32
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ var sockjs = require("sockjs");
77
var StreamCache = require("stream-cache");
88
var http = require("http");
99
var https = require("https");
10-
var httpProxy = require("http-proxy");
11-
var proxy = new httpProxy.createProxyServer({secure: false});
10+
var httpProxyMiddleware = require("http-proxy-middleware");
1211
var serveIndex = require("serve-index");
1312
var historyApiFallback = require("connect-history-api-fallback");
1413

@@ -120,42 +119,69 @@ function Server(compiler, options) {
120119

121120
proxy: function() {
122121
if (options.proxy) {
123-
if (!Array.isArray(options.proxy)) {
124-
options.proxy = Object.keys(options.proxy).map(function (path) {
122+
/**
123+
* Assume a proxy configuration specified as:
124+
* proxy: 'a url'
125+
*/
126+
if(typeof options.proxy === 'string') {
127+
options.proxy = [{
128+
context: options.proxy
129+
}];
130+
}
131+
132+
/**
133+
* Assume a proxy configuration specified as:
134+
* proxy: {
135+
* 'context': { options }
136+
* }
137+
* OR
138+
* proxy: {
139+
* 'context': 'target'
140+
* }
141+
*/
142+
if(!Array.isArray(options.proxy)) {
143+
options.proxy = Object.keys(options.proxy).map(function (context) {
125144
var proxyOptions;
126-
if (typeof options.proxy[path] === 'string') {
127-
proxyOptions = {path: path, target: options.proxy[path]};
128-
} else {
129-
proxyOptions = options.proxy[path];
130-
proxyOptions.path = path;
145+
146+
if(typeof options.proxy[context] === 'string') {
147+
proxyOptions = {
148+
context: context,
149+
target: options.proxy[target]
150+
};
131151
}
152+
else {
153+
proxyOptions = options.proxy[context];
154+
proxyOptions.context = context;
155+
}
156+
132157
return proxyOptions;
133158
});
134159
}
135-
options.proxy.forEach(function (proxyOptions) {
136-
proxyOptions.ws = proxyOptions.hasOwnProperty('ws') ? proxyOptions.ws : true;
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.sockWrite(this.sockets, "proxy-error", [msg]);
150-
res.statusCode = 502;
151-
res.end();
152-
}.bind(this));
153-
if (proxyOptions.configure) {
154-
proxyOptions.configure(proxy);
160+
161+
/**
162+
* Assume a proxy configuration specified as:
163+
* proxy: [
164+
* {
165+
* context: ...,
166+
* ...options...
167+
* }
168+
* ]
169+
*/
170+
options.proxy.forEach(function (proxyConfig) {
171+
var context = proxyConfig.context || proxyConfig.path;
172+
173+
app.use(function(req, res, next) {
174+
if(typeof proxyConfig.bypass === 'function') {
175+
var bypassUrl = proxyConfig.bypass(req, res, proxyConfig) || false;
176+
177+
if(bypassUrl) {
178+
req.url = bypassUrl;
155179
}
156180
}
157-
}.bind(this));
158-
}.bind(this));
181+
182+
next();
183+
}, httpProxyMiddleware(context, proxyConfig));
184+
});
159185
}
160186
}.bind(this),
161187

@@ -296,7 +322,7 @@ Server.prototype.listen = function() {
296322
this.sockets.splice(connIndex, 1);
297323
}
298324
}.bind(this));
299-
325+
300326
if(this.hot) this.sockWrite([conn], "hot");
301327
if(!this._stats) return;
302328
this._sendStats([conn], this._stats.toJson(), true);

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
"compression": "^1.5.2",
1111
"connect-history-api-fallback": "1.1.0",
1212
"express": "^4.13.3",
13-
"http-proxy": "^1.11.2",
1413
"optimist": "~0.6.0",
1514
"serve-index": "^1.7.2",
1615
"sockjs": "^0.3.15",
1716
"sockjs-client": "^1.0.3",
1817
"stream-cache": "~0.0.1",
1918
"strip-ansi": "^3.0.0",
2019
"supports-color": "^3.1.1",
21-
"webpack-dev-middleware": "^1.4.0"
20+
"webpack-dev-middleware": "^1.4.0",
21+
"http-proxy-middleware": "~0.9.0"
2222
},
2323
"devDependencies": {
2424
"css-loader": "~0.23.0",

0 commit comments

Comments
 (0)