Rubycut
Rubycut

Reputation: 1646

How to merge webpack dev server proxy urls

I need to set header for my webpack dev server paths, but as you can see for this config, I have to copy proxy configs for each url I want to specify, is there any way to DRY this config?

  devServer: {
port: 3120,
host: "10.0.0.46",
publicPath: "http://10.0.0.46:3102/dist/js/",
hot: true,
compress: true,
contentBase: path.join(__dirname, "public"),
proxy: {
  "/customer/x": {
    target: "http://localhost:3100",
    secure: false,
    onProxyReq: function (proxyReq, req, res) {
      proxyReq.setHeader('X-Forwarded-User', 'user');
    }
  },
  "/cluster/**": {
      target: "http://localhost:3100",
      secure: false,
      onProxyReq: function (proxyReq, req, res) {
        proxyReq.setHeader('X-Forwarded-User', 'user');
      }
  },
  "/server/**": {
      target: "http://localhost:3100",
      secure: false,
      onProxyReq: function (proxyReq, req, res) {
        proxyReq.setHeader('X-Forwarded-User', 'user');
      }
  },
  "/data": {
      target: "http://localhost:3100",
      secure: false,
      onProxyReq: function (proxyReq, req, res) {
        proxyReq.setHeader('X-Forwarded-User', 'user');
      }
  },
  "/graph": {
      target: "http://localhost:3100",
      secure: false,
      onProxyReq: function (proxyReq, req, res) {
        proxyReq.setHeader('X-Forwarded-User', 'user');
      }
  }
}

}

As you can see, all proxy config setups are the same, except for url part.

My versions are:

"webpack": "^2.2.1"
"webpack-dev-server": "^2.4.5"

Upvotes: 1

Views: 2269

Answers (1)

Pruthvi
Pruthvi

Reputation: 66

If you want to proxy multiple, specific paths to the same target, you can use an array of one or more objects with a context property:

proxy: [{
  context: ["/auth", "/api"],
  target: "http://localhost:3000",
  secure: false,
  onProxyReq: function (proxyReq, req, res) {
    proxyReq.setHeader('X-Forwarded-User', 'user');
  }
}]

Upvotes: 5

Related Questions