NodeJs Proxy Server Connections

168 views Asked by At

I have written a code so that the the main server connects to the proxy server and it goes on. I need the server to proxy the requests asynchronously but the problem I am facing is that I need to refresh the page. Here is the code

var dirServer = httpProxy.createServer(function (req, res, proxy) {         
var randNum = Math.floor(Math.random()*6);            
console.log(randNum);        
 proxy.proxyRequest(req, res, {
    host: creds[randNum].host,
    port: creds[randNum].port
  });
 console.log("Proxying the request to host : " + creds[randNum].host + ' ' + "Proxying the request to port" + creds[randNum].port);
}).listen(7874, "0.0.0.0");

I have a creds.json file in which it has the names, host and ports of the server.

1

There are 1 answers

1
Jigar Jain On

If I have understood your problem correctly, you want a new proxy connection to be made every x seconds, the following code should help

setInterval(function() {
    var dirServer = httpProxy.createServer(function (req, res, proxy) {
    var randNum = Math.floor(Math.random()*6);
    console.log(randNum);
    proxy.proxyRequest(req, res, {
        host: creds[randNum].host,
        port: creds[randNum].port
    });
    console.log("Proxying the request to host : " + creds[randNum].host + ' ' + "Proxying the request to port" + creds[randNum].port);
    }).listen(7874, "0.0.0.0");
}, 2000);