Search code examples
webpackwebpack-dev-servernode-http-proxy

Remove matched `/api` path in Webpack proxy


I have a backend service running at http://localhost:9090/. All calls starting with /api/* should be forwarded to it, without the /api in front of it. So when I call http://localhost:8080/api/my/route it should proxy to http://localhost:9090/my/route.

If I use the following option:

proxy : [{
  path : '/api/*',
  target : 'http://localhost:9090'
}]

When calling http://localhost:8080/api/my/route, the backend service complains that it cannot find route /api/my/route.

The documentation suggests that I could use any options from node-http-proxy, but I can't figure out the correct options to use.

What options do I need to use to get the desired result?


Solution

  • As of webpack-dev-server version >= 1.15.0, you can use the documented pathRewrite:

    proxy: {
      '/api': {
        target: 'https://other-server.example.com',
        pathRewrite: {'^/api' : ''}
      }
    }