Skip to content

Commit 755da19

Browse files
committed
Merge pull request #127 from sakabako/master
Add proxy to allow working with a back end on a different server/port
2 parents a5a9e2e + 08981ac commit 755da19

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

bin/webpack-dev-server.js

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

lib/Server.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,31 @@ function Server(compiler, options) {
112112
if (options.historyApiFallback) {
113113
// Fall back to /index.html if nothing else matches.
114114
app.use(historyApiFallback);
115-
// include our middleware to ensure it is able to handle '/index.html' requrst after redirect
115+
// include our middleware to ensure it is able to handle '/index.html' requrst after redirect
116116
app.use(this.middleware);
117117
}
118118

119+
if (options.proxy) {
120+
var paths = Object.keys(options.proxy);
121+
paths.forEach(function (path) {
122+
var proxyOptions;
123+
if (typeof options.proxy[path] === 'string') {
124+
proxyOptions = {target: options.proxy[path], ws: true};
125+
} else {
126+
proxyOptions = options.proxy[path];
127+
}
128+
app.all(path, function (req, res) {
129+
proxy.web(req, res, proxyOptions);
130+
});
131+
});
132+
}
133+
119134
if(options.contentBase !== false) {
120135
var contentBase = options.contentBase || process.cwd();
121136

122137
if(typeof contentBase === "object") {
138+
console.log('Using contentBase as a proxy is deprecated and will be removed in the next major version. Please use the proxy option instead.\n\nTo update remove the contentBase option from webpack.config.js and add this:');
139+
console.log('proxy: {\n\t"*": [your current contentBase configuration]\n}');
123140
// Proxy every request to contentBase.target
124141
app.all("*", function(req, res) {
125142
proxy.web(req, res, contentBase, function(err) {

0 commit comments

Comments
 (0)