I m actually trying to developp a node-http-proxy for my server and I m facing problems. In fact, my applications are created like this :
On the /var/www I have a folder corresponding to the host name on my server : example.co, in it, I have :
My first question is how can I redirect EVERY request to the endpoint corresponding. Explanation :
The problem is that if I try to access www.example.co/webservice/users when I m not logged in, I will be redirected to www.example.co/auth and not www.example.co/webservice/auth
My second question is that, on some webservices (done with other technologies like PHP) I have an Access-Control-Allow-Origin error. Even if, in the proxy I set the header, I have the error. Any idea ?
My last question is : how to implement a proxy.ws() to redirect the request to a special port ?
This is my node-http-proxy implementation :
var Server = require('./config/config-web');
var http = require('http'),
httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({target: {host: 'localhost', port: Server.port}});
var Routes = require('./config/route');
var url = require('url');
/*HTTP PROXY*/
var server = http.createServer(function (req, res) {
var hostname = req.headers.host.split(":")[0];
var pathname = url.parse(req.url).pathname;
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', '*');
res.setHeader('Access-Control-Allow-Headers', '*');
var route = contains(hostname);
if (route) {
proxy.web(req, res, { target: route.target });
} else {
res.end("Not found");
}
});
function contains(route) {
for (r in Routes) {
if (Routes[r].hostSource === route) {
return Routes[r];
}
}
return undefined;
}
console.log('[PROXY] Listening on port ' + Server.port);
server.listen(Server.port);
And this is an example of the the config/route module :
module.exports = [
{hostSource: "example.co", target: "http://example.co:20000"},
{hostSource: "example.co/webservices", target: "http://example.co:20001/"}
]
I hope to have been clear and wish you can help me.
Thanks for advance