proxy does not work for web pack
Hi all, apologize for my stupid question.
currently I am having difficulty in setting up proxy in the webpack.config.js.
const webpackConfig = { name : 'client', target : 'web', devtool : config.compiler_devtool, resolve : { root : paths.client(), extensions : ['', '.js', '.jsx', '.json'] }, devServer: { historyApiFallback: true, contentBase: './', proxy: {'/api/': 'http://localhost:9000'} }** module : {} }
with the above configuration, my request to /api does not seem to be redirected to localhost:9000. Is there anything that I did wrong?
This is my fault, sorry. I just recently started refactoring parts of this project and have not yet put any middleware back in for proxying requests. If you take a look at the server code from our last tagged release[1] you can do the same thing but with the express middleware[2]:
- [1] https://github.com/davezuko/react-redux-starter-kit/blob/v3.0.0-alpha.2/server/main.js#L17-L20
- [2] https://github.com/chimurai/http-proxy-middleware
At this point I have to decide whether to fix it in the project or to remove the configuration that implies proxying is possible. Personally I think it's a bit unnecessary to include out of the box, since you can just point your requests where you need to from the client, but it's also not much to provide by default. Either way, until then, the above should unblock you.
Cool!! It works with the above settings.
I have NOT been able to get this express proxy to work using the example
I edited server/main.js:
const proxy = require('http-proxy-middleware'); if (config.env === 'development') { const compiler = webpack(webpackConfig)
debug('Enable webpack dev and HMR middleware') app.use(require('webpack-dev-middleware')(compiler, { publicPath : webpackConfig.output.publicPath, contentBase : paths.client(), hot : true, quiet : config.compiler_quiet, noInfo : config.compiler_quiet, lazy : false, stats : config.compiler_stats })) app.use(require('webpack-hot-middleware')(compiler))
// Serve static assets from ~/src/static since Webpack is unaware of // these files. This middleware doesn't need to be enabled outside // of development since this directory will be copied into ~/dist // when the application is compiled.
proxy('http://localhost:8080/api'); app.use(express.static(paths.client('static'))) }
Chrome says: Warning: [react-router] Location "/api" did not match any routes
Ok it does work but needs to be in order:
//server/main.js: app.use(require('connect-history-api-fallback')())
// proxy middleware options - Enable api-proxy if it has been enabled in the config. if (config.proxy && config.proxy.enabled) { const apiProxy = proxy('/api', {target: config.proxy.options.host}); app.use(apiProxy) }
//config/environments.js
development: (config) => ({
compiler_public_path: http://${config.server_host}:${config.server_port}/,
proxy: {
enabled: true,
options: {
host: 'http://localhost:8080',
}
}
})
Closing per the above workarounds and due to the fact that proxy support was temporarily dropped and has not been officially added back in, mostly due to limitations on what free time I have available to support this project. Sorry about that!
@davezuko David, would be really nice from you to update the description of this boilerplate with some note about the absence of the proxy support! Could save a lot of time and efforts.
@polyakoff you are completely right, sorry for the confusion. I'm currently working on a webpack 2 branch that will include a full rewrite of the README, and I'll be sure to update the proxy information.
@davezuko Doesn't webpack dev server support proxy by default? https://webpack.github.io/docs/webpack-dev-server.html#proxy
This used to work in a much older version of your starter kit:
devServer: {
proxy: {
'/api/*': {
target: 'http://other-server.example.com',
secure: false
}
}
}
Was this explicitly disabled?