对本地文件的代理请求

问题描述 投票:0回答:1

我有这样的代理配置:

proxy: {
  "/api/smth": {
    target: "http://api.example.com/",
    secure: false,
    changeOrigin: true,
  },
}

现在我想将api调用/api/*/meta重定向到本地文件%PROJ_ROOT%/meta/*.json

我该如何配置?

webpack proxy webpack-dev-server http-proxy-middleware
1个回答
0
投票
proxy: {
  "/api/*/meta": {
    selfHandleResponse: true,
    bypass(req, resp) {
      const id = req.url.match(/api\/([-\w]+)\/meta/)[1];
      resp.header("Content-Type", "application/json");
      fs.createReadStream(`./meta/${id}.json`).pipe(resp);
    },
  },
  "/api/smth": {
    target: "http://api.example.com/",
    secure: false,
    changeOrigin: true,
  },
}
© www.soinside.com 2019 - 2024. All rights reserved.