Node-http-proxy different port, different subdomain and websockets

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 :

  • Presentation Layer : AngularJS on apache on port 80
  • Business Layer : NodeJS REST Api on random port accessible by http://www.example.co on a port
  • Socket.io (on business Layer) on another random port http://www.example.co on anotherport

On the /var/www I have a folder corresponding to the host name on my server : example.co, in it, I have :

  • presentation folder
  • business folder

My first question is how can I redirect EVERY request to the endpoint corresponding. Explanation :

  • I have declared a rule that tells me that if I access www.example.co on port 80, I redirect the request to my local Presentation layer (so on localhost on apache, to my subdomain)
  • I have declared another rule that tells me that if I access www.example.co:80/webservice, I redirect the request to my webservice at localhost:port-of-the-service
  • I have WebServices that redirects user to an authentication page when he's not logged.

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