Skip to content

Add proxy to allow working with a back end on a different server/port #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions bin/webpack-dev-server.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,18 @@ new Server(webpack(wpOpt), options).listen(options.port, options.host, function(
console.log("content is served from " + options.contentBase);
if(options.historyApiFallback)
console.log("404s will fallback to /index.html");

if (options.proxy) {
var paths = Object.keys(options.proxy);
paths.forEach(function (path) {
var target;
if (typeof options.proxy[path] === 'string') {
target = options.proxy[path];
} else {
target = options.proxy[path].target;
}
console.log('proxying '+path+' to '+target);
});
}

});
19 changes: 18 additions & 1 deletion lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,31 @@ function Server(compiler, options) {
if (options.historyApiFallback) {
// Fall back to /index.html if nothing else matches.
app.use(historyApiFallback);
// include our middleware to ensure it is able to handle '/index.html' requrst after redirect
// include our middleware to ensure it is able to handle '/index.html' requrst after redirect
app.use(this.middleware);
}

if (options.proxy) {
var paths = Object.keys(options.proxy);
paths.forEach(function (path) {
var proxyOptions;
if (typeof options.proxy[path] === 'string') {
proxyOptions = {target: options.proxy[path], ws: true};
} else {
proxyOptions = options.proxy[path];
}
app.all(path, function (req, res) {
proxy.web(req, res, proxyOptions);
});
});
}

if(options.contentBase !== false) {
var contentBase = options.contentBase || process.cwd();

if(typeof contentBase === "object") {
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:');
console.log('proxy: {\n\t"*": [your current contentBase configuration]\n}');
// Proxy every request to contentBase.target
app.all("*", function(req, res) {
proxy.web(req, res, contentBase, function(err) {
Expand Down