To avoid same-domain AJAX issues, I want my node.js web server to forward all requests from URL /api/BLABLA
to another server, for example other_domain.com:3000/BLABLA
, and return to user the same thing that this remote server returned, transparently.
All other URLs (beside /api/*
) are to be served directly, no proxying.
How do I achieve this with node.js + express.js? Can you give a simple code example?
(both the web server and the remote 3000
server are under my control, both running node.js with express.js)
So far I found this https://github.com/http-party/node-http-proxy , but reading the documentation there didn’t make me any wiser. I ended up with
var proxy = new httpProxy.RoutingProxy();
app.all("/api/*", function(req, res) {
console.log("old request url " + req.url)
req.url="https://stackoverflow.com/" + req.url.split("https://stackoverflow.com/").slice(2).join("https://stackoverflow.com/"); // remove the '/api' part
console.log("new request url " + req.url)
proxy.proxyRequest(req, res, {
host: "other_domain.com",
port: 3000
});
});
but nothing is returned to the original web server (or to the end user), so no luck.