如何合并webpack dev服务器代理URL

时间:2017-07-04 10:18:19

标签: webpack webpack-dev-server webpack-2

我需要为我的webpack dev服务器路径设置标头,但正如你可以看到这个配置,我必须为我想指定的每个网址复制代理配置,有没有办法干掉这个配置?

  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');
      }
  }
}

}

如您所见,除了url部分,所有代理配置设置都是相同的。

我的版本是:

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

1 个答案:

答案 0 :(得分:2)

如果要代理同一目标的多个特定路径,可以使用具有上下文属性的一个或多个对象的数组:

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