简体   繁体   中英

NodeJs Proxy Server Connections

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM