Error while trying to re-direct application to new server port

134 views Asked by At

Im creating new server to listen to new port(with the second create below),now when I call to the application with some port I want to redirect it to the new created server port, and put the message in the browser "Request route to on 9009"

I use the following code to create server

httpProxy = require('http-proxy');

proxy = httpProxy.createProxyServer({});

    http.createServer(function (req, res) {
            var hostname = req.headers.host.split(":")[0];
             if  (hostname ==='localhost') {
              proxy.web(req, res, {target: 'http://localhost:9009'});
             }     
}    }).listen(3000, function () {
        console.log('App listening on port 3000');
    });
now I create the new server

http.createServer(function (req, res) {
 res.writeHead(302, {
    'Location': 'http://localhost:9009'
 });

   res.end("Request route to  9009");
}).listen(9009);

now when I put localhost:3000 it redirects me to localhost:9009 (which is exactly what I need I can see in the browser) but I got error This webpage has a redirect loop ERR_TOO_MANY_REDIRECTS

If I remove from the second createServer function the following

res.writeHead(302, {
    'Location': 'http://localhost:9009'
 }); 

it is not redirects and I dont got the error... did I put the this code in the wrong place?or there is a way to do it diffrent? I use https://github.com/nodejitsu/node-http-proxy

update

I change the code to following

var http = require('http');

http.createServer(function (req, res) {
    console.log("Server created");
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.write('9009 here' + '\n' + JSON.stringify(req.headers, true, 2));
    res.end();
}).listen(9009);

http.createServer(function(req, res) {
    console.log("Server 2 created");
    res.writeHead(302, {
        'Location': 'http://localhost:9009/'
    });
    res.end("Request route to 9009");
}).listen( 3001 );
1

There are 1 answers

2
dandavis On BEST ANSWER

in the code you show, the proxy make port 9009 show up on port 3000, more like an apache url rewrite than a url redirect.

if you want to send visitors landing on port 3000 to port 9009, simple http is plenty:

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('9009 here' + '\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen( 9009 );

http.createServer(function(req, res) {
    res.writeHead(302, {
        'Location': 'http://X.X.X.X:9009/' // fix me
     });
    res.end("Request route to  9009");
}).listen( 3000 );

if you want every visitor to a new personal port, this is a simple but naive way to do so (not accounting for repeated ports, which will crash node 1/1000 times):

var http = require('http');
http.createServer(function(req, res) {
    var port=Math.floor(Math.random()*1000)+12000;
    http.createServer(function (req, res) {
      res.writeHead(200, { 'Content-Type': 'text/plain' });
      res.write( port + ' talking' + '\n' + JSON.stringify(req.headers, true, 2));
      res.end();
    }).listen( port );

    res.writeHead(302, {
        'Location': 'http://X.X.X.X:'+ port +'/'  // fix me
     });
    res.end("Request route to " + port );
}).listen( 3000 );