0

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 );
07_05_GuyT
  • 2,787
  • 14
  • 43
  • 88
  • why do you proxy port 9009 to port 3000 if you want the client to redirect to 9009? when you proxy, you make the content available at port 9009 also available at port 3000. if you simply want to send traffic from port 3000 to another port, don't use a proxy, just http redirect them. – dandavis Jun 24 '15 at 05:44
  • do you want every visitor to get redirected to a new unique port, or all visitors to port 9009? – dandavis Jun 24 '15 at 05:51
  • @dandavis-lets assume for now that the app will route to 9009 is it possible?can you help how to do that? – 07_05_GuyT Jun 24 '15 at 05:57

1 Answers1

1

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 );
dandavis
  • 16,370
  • 5
  • 40
  • 36
  • Thanks a lot this is working,Voted up! I've two more question 1. when I hit in the browser on localhost:3000 I saw in the console Server 2 created Server created Server created this 3 entries and when I click on 9009 I saw twice server created ,is it ok the 3 and 2 calles ? 2. if I want to put some logic that will handle the URL from the post and get call where you suggest to do it ? – 07_05_GuyT Jun 24 '15 at 07:00
  • 1. i am not sure what you are asking. the first code should make two servers for ten clients, the second code should make eleven servers for ten clients. i would not use the second code, unless you write a lot more code to handle turning off the servers when the client is done. 2. i think POST has trouble with redirects, so you want to put that code in the port 9009 server. – dandavis Jun 24 '15 at 07:56