Last active
January 31, 2016 20:58
-
-
Save klzns/e0ba0979014a62a1ff1c to your computer and use it in GitHub Desktop.
How to proxy index.html with react-transform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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