I am using http-proxy-middleware to proxy my API calls
How can I proxy multiple target hosts? I have searched through the issue and still could not understand.
https://github.com/chimurai/http-proxy-middleware/issues/12 - Allow me to use proxy table but what should link should I put for target
? Because I have multiple targets.
I am thinking of pattern matching to proxy multiple hosts but another problem would arise because I have a few hosts with almost the same URL link.
Example
- https://website1.com/api/v1.1/{endpoint}
- https://website2.com/api/{endpoint}
- https://api.website3.com/api/{endpoint}
Tried some solutions below but dosen't work
Solution 1
const proxyOptions = proxy({
target: ["https://website1.com", "https://website2.com", "https://api.website3.com"],
changeOrigin: true,
loglevel: "debug"
});
Solutuon 2
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"
});
Solution 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 })
);
Mounting the proxy
server.use("/api", proxyOptions)
Thanks for looking into this question!