Skip to content

Instantly share code, notes, and snippets.

@klzns
Last active January 31, 2016 20:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save klzns/e0ba0979014a62a1ff1c to your computer and use it in GitHub Desktop.
Save klzns/e0ba0979014a62a1ff1c to your computer and use it in GitHub Desktop.
How to proxy index.html with react-transform
var express = require('express');
var webpack = require('webpack');
var httpProxy = require('http-proxy');
var proxy = new httpProxy.createProxyServer();
var config = require('./webpack.config');
var app = express();
var compiler = webpack(config);
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}));
app.use(require('webpack-hot-middleware')(compiler));
if (config.proxy) {
var paths = Object.keys(config.proxy);
paths.forEach(function (path) {
var proxyOptions;
if (typeof config.proxy[path] === 'string') {
proxyOptions = {target: config.proxy[path], ws: true};
} else {
proxyOptions = config.proxy[path];
}
app.all(path, function (req, res) {
proxy.web(req, res, proxyOptions);
});
});
}
app.listen(3000, 'localhost', function(err) {
if (err) {
console.log(err);
return;
}
console.log('Listening at http://localhost:3000');
});
var path = require('path');
var webpack = require('webpack');
module.exports = {
devtool: 'eval',
entry: [
'webpack-hot-middleware/client',
'./src/index'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/static/'
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
],
module: {
loaders: [{
test: /\.js$/,
loaders: ['babel'],
include: path.join(__dirname, 'src')
}]
},
proxy: {
'*': 'http://mybackendserver.com/'
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment