I want to allow an authenticated client in Express to access to other web applications that are running on the server, but on different ports.
For example, I have express running on http://myDomain
and I have another application running on say port 9000
. I want to be able to reach the other app through http://myDomain/proxy/9000
.
I had a little bit of success using node-http-proxy, for example:
function(req, res) {
var stripped = req.url.split('/proxy')[1];
var path = stripped.split('/');
var port = path.shift();
var url = path.join('/');
req.url = url;
proxy.web(req, res, {
target: 'http://127.0.0.1:' + port
});
}
However, the big problem is that when the web app makes GET requests, such as for /js/lib.js
, it resolves to http://myDomain/js/lib.js
, which is problematic because express is not aware of those assets. The correct request would be to http://myDomain/proxy/9000/js/lib.js
. How do I route all these additional requests?