代理多个具有相同URL模式的主机

3
我正在使用http-proxy-middleware代理我的API调用。
我如何代理多个目标主机?我已经查阅了相关问题,但仍然无法理解。
参考https://github.com/chimurai/http-proxy-middleware/issues/12,允许我使用代理表,但是target应该填写什么链接呢?因为我有多个目标主机。
我考虑使用模式匹配来代理多个主机,但会出现另一个问题,因为我有几个主机的URL链接几乎相同。
例如: 我尝试以下解决方案但不起作用:
解决方案 1
const proxyOptions = proxy({
  target: ["https://website1.com", "https://website2.com", "https://api.website3.com"],
  changeOrigin: true,
  loglevel: "debug"
});

方案二
const proxyTable = {
  "/api": ["https://website1.com", "https://website2.com", "https://api.website3.com"]
};

const proxyOptions = proxy({
  target: "http://localhost:3000",
  router: proxyTable,
  changeOrigin: true,
  loglevel: "debug"
});

解决方案3
server.use(
      proxy("/api", { target: "https://website1.com", changeOrigin: true }),
      proxy("/api", { target: "https://website2.com", changeOrigin: true }),
      proxy("/api", { target: "https://api.website3.com", changeOrigin: true })
    );

安装代理

server.use("/api", proxyOptions)

感谢您关注这个问题!
1个回答

1

我使用 pathRewrite 得到了解决方案。

const website1 = proxy({
  target: "https://website1.com",
  changeOrigin: true,
  pathRewrite: {
    "^/website1": "/api"
  },
  loglevel: "debug"
});

const website2 = proxy({
  target: "https://website2.com",
  changeOrigin: true,
  pathRewrite: {
    "^/website2": "/api"
  },
  loglevel: "debug"
});

const website3 = proxy({
  target: "https://api.website3.com",
  changeOrigin: true,
  pathRewrite: {
    "^/website3": "/api"
  },
  loglevel: "debug"
});

安装服务器。
const server = express();

server.use("/website1", website1);
server.use("/website2", website2);
server.use("/website3", website3);

然而,这将会给我的网站带来许多我不需要的页面,例如http://localhost:3000/website1等等。
有没有办法隐藏这些页面或者显示其他内容代替我正在使用的网站主页?
抱歉,我还在学习node.js,请耐心等待。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接