简体   繁体   中英

how to make the page not redirect when using webpack's devServer.proxy?

here's a project whose router is in the backend , image we can open the url www.example.com/a.htm . Then I want to use webpack to modularize the js, as you know, all the requests should be redirected to the www. example.com www. example.com instead of localhost .

Then I find the devServer.proxy :

devServer: {
  proxy: {
    "/": {
      target: "http://www.example.com",
      changeOrigin: true
    }
  }
}

but when I open localhost:8080 , it will be redirected to www.example.com , and then it cannot find the bundle.js file.

So here's my question, how to make localhost:8080 not redirected? or it's wrong to use it this way?

I think this option is reason why it's not worked

https://webpack.js.org/configuration/dev-server/#devserver-publicpath-

If I set publicPath to something else not / (it's by default) proxy server work with root path.

I think becouse this path busy(

UPDATE:

But usually need return index.html file on / ...

So, I find solution:

  1. Set publicPath: '/_web_/' for example
  2. After /?api=for_example proxy with filter-context {context: (path, req) => req.query && req.query.api, target: 'http://api.localhost:8080'}
  3. Set proxy from / to /_web_/ with next config {context: '/', target: 'http://localhost:80', pathRewrite: {'^/' : '/_web_/'}

Now youre weback-dev-server work: if api in query exist - return info from api.localhost:8080 else return info/file from /_web_/ .

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM