Skip to content

Commit dad0612

Browse files
committed
Merge pull request #187 from nthtran/master
added support for regex path and rewrite in proxy options
2 parents 8e8f540 + d80ea0f commit dad0612

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

bin/webpack-dev-server.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,4 @@ new Server(webpack(wpOpt), options).listen(options.port, options.host, function(
139139
console.log("content is served from " + options.contentBase);
140140
if(options.historyApiFallback)
141141
console.log("404s will fallback to /index.html");
142-
143-
if (options.proxy) {
144-
var paths = Object.keys(options.proxy);
145-
paths.forEach(function (path) {
146-
var target;
147-
if (typeof options.proxy[path] === 'string') {
148-
target = options.proxy[path];
149-
} else {
150-
target = options.proxy[path].target;
151-
}
152-
console.log('proxying '+path+' to '+target);
153-
});
154-
}
155-
156142
});

lib/Server.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,22 @@ function Server(compiler, options) {
110110
}.bind(this));
111111

112112
if (options.proxy) {
113-
var paths = Object.keys(options.proxy);
114-
paths.forEach(function (path) {
115-
var proxyOptions;
116-
if (typeof options.proxy[path] === 'string') {
117-
proxyOptions = {target: options.proxy[path], ws: true};
118-
} else {
119-
proxyOptions = options.proxy[path];
120-
}
121-
app.all(path, function (req, res) {
113+
if (!Array.isArray(options.proxy)) {
114+
options.proxy = Object.keys(options.proxy).map(function (path) {
115+
var proxyOptions;
116+
if (typeof options.proxy[path] === 'string') {
117+
proxyOptions = {path: path, target: options.proxy[path]};
118+
} else {
119+
proxyOptions = options.proxy[path];
120+
proxyOptions.path = path;
121+
}
122+
return proxyOptions;
123+
});
124+
}
125+
options.proxy.forEach(function (proxyOptions) {
126+
proxyOptions.ws = proxyOptions.hasOwnProperty('ws') ? proxyOptions.ws : true;
127+
app.all(proxyOptions.path, function (req, res) {
128+
if(typeof proxyOptions.rewrite === 'function') proxyOptions.rewrite(req, proxyOptions);
122129
if (proxyOptions.host) {
123130
req.headers.host = proxyOptions.host;
124131
}

0 commit comments

Comments
 (0)