-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Conversation
…. Print the proxy targets on startup.
Ok, we could remove the |
Absolutely. Should this documentation be updated too? |
@sokra I added them, not sure if you want them styled in any specific way. |
+1 here |
} else if(/^(https?:)?\/\//.test(contentBase)) { | ||
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"*": "'+contentBase+'"\n}'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong here. contentBase
is not used as proxy. It's used to redirect requests, which is not deprecated.
@sokra I removed the warning for non-deprecated redirects |
Add proxy to allow working with a back end on a different server/port
Thanks |
@sakabako could you provide example of how to use this new option? |
The following two setup should work: // webpack.config.js
devServer: {
proxy: {
'*': 'http://localhost:5000'
}
} and // webpack.config.js
devServer: {
proxy: {
'*': {target: 'http://localhost:5000'}
}
} However, the deprecation warning for
So I put this in my config and the proxy does not work: proxy: {
"*": [{target: 'http://localhost:5000'}]
} Since |
|
Haha -- hey, @sakabako! Just setting this up now :) |
I've added a pull request to make this support wildcards/regular expressions in the proxy configuration. See #148. ex:
Thanks, |
Unfortunately I cant get this working with my contentBase setup. I know that contentBase will be deprecated, but with proxies it seems that I loose the capacity to serve static files. And if I set up both: contentbase & proxy, the proxy is never called. Here my Setup: devServer = new webpackDevServer(webpack(webpackConfig),{
contentBase: './build/',
hot: true,
watchDelay: 100,
proxy: {
"/api": "localhost:9091",
//"/": {
// target: '/build/'
//}
},
noInfo: true
}); |
My solution will probably work for you, because you can selectively proxy subpaths with wildcards. You can test it by doing an
|
Does anyone have a working solution for serving static files using Just a quick note to @jamon, not contributing to the issue at hand at all 😆 |
Using contentBase as a proxy is deprecated, using it to serve files or redirect is not. @KeKs0r, I'm not sure if you're having trouble with the proxy or serving static files. Try |
@sakabako Ah, I misunderstood then. Thanks for the swift response! |
I think Also there is no option for adding headers, which might be useful quite often. The backend can expect a |
For those interested, I've updated my related pull request to allow rewriting the request. This should solve your issue @pvolok. Example: utility for rewriting URLs
config
*note: fixed bug in example code above |
Huge +1 for @jamon's patch, at least the RegExp support. I was already able to use it to have koa serve my static assets while using React to render the page dynamically and have webpack-dev-server and react-hot-loader do HMR using this (partial) config: config.devServer = {
contentBase: config.output.path,
filename: 'assets/scripts/application.js',
host: 'localhost',
hot: true,
inline: true,
// webpack-dev-server running on 3002
port: 3002,
progress: true,
publicPath: config.output.publicPath,
stats: { colors: true },
proxy: [{
// proxy all requests not containing ".hot-update.js"
// regex is still crappy because JS doesn't have negative lookbehind
path: /^(?!.*\.hot-update\.js)(.*)$/,
// koa running on 3001 with koa-send and isomorphic react
target: 'http://localhost:3001/'
}]
}; |
I have tried all above configurations and none of them works for me. All calls to the API through proxy have 404. Has anyone had a working github project to use api proxy? Thanks. |
https://github.com/SimenB/webpack-fun/ That worked with an extremely simple express backend a couple months ago. Haven't tested since, but should still be good. We have a complete working sample at work, I can send relevant parts tomorrow if you still need it. EDIT: All we do is new WebpackDevServer(compiler, {
hot: true,
inline: true,
proxy: {
'*': 'http://localhost:7021'
},
stats: { colors: gutil.colors.supportsColor }
} With The project is closed source, so I can't link to the complete config |
@SimenB Thanks for the quick reply and the working example. I tried your proxy settings before and it did work; however, all request goes to the API server including request for react javascript. I am using webpack.config.js and use npm start to start webpack-dev-server. I think the difference is you are using gulp to start webpack-dev-server. I will clone your repo and dig into the detail. What I want to do is the proxy all requests to /api/rs to my API server and everything else to the webpack-dev-server. I call the API from react with superagent. |
@stevehu Hi I have the same issue as you. Have you found a solution ? |
@lauterry Yes. It works for me now. You can find a working example from my react shopping cart repo https://github.com/networknt/light/tree/master/efg |
@KeKs0r @SimenB @sakabako
|
I think the purpose of this PR got missed with my last one. I'm reopening because this is a feature that either makes or breaks the server for me and I'm sure many others. I've implemented this many times and seen it implemented even more. I also changed the configurations to be more flexible.
This adds an option to proxy a single path to an arbitrary server. This is different than the contentBase setting, which can proxy all requests or none. Unless I'm missing a configuration, this is something that is not possible with the server as it stands.
Here are some use cases that make this feature worthwhile:
Without a built in proxy this must be done with nginx or a proxy above this server, which makes changing paths tedious and means you can't just clone and spin up another instance of your app.