node.jsproxydoublenode-http-proxy

(double proxy) node js create proxy server behind corporate proxy using node-http-proxy


I have anodejs application that uses node-http-proxy to create a proxy to send the incoming requests for example: http://localhost/api/login to https://server1/api/login. here is the code used :

var httpProxy = require('http-proxy');

var proxy = httpProxy.createProxyServer();

self.routes['/api/*'] = function(req, res) {
        proxy.proxyRequest(req, res, 
        {
            target: "https://server1",
            changeOrigin: true
        });
    };

This is working just fine in my machine. Now when i deploy this on a server, i get error:

{ [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }

The problem is that there is another proxy (corporate proxy called: localProxy) between myserver and server1.

I don't know where to set the localProxy in my code above. and where to set the server1 url?

and is there is a way to use node-http-proxy in this case??


Solution

  • the answer for me was stop using proxy module and make the proxy operation manually.

    please find code below:

    self.app.all('/blabla/*',function(req,res) {
        console.log("inside /blabla request");
    
    
      if(req.method=="GET"){
         console.log(req.url);
         var headers = {
    
            'Content-Type': 'application/json',
            'Content-Length': req.body.length ,
            'Accept': '*/*',
            'X-Requested-With':'XMLHttpRequest'
          };
    
    
    
        request({
          url: "http://YOUR_EXTERNAL_SERVER"+req.url,
          method: "GET",
          timeout: 10000,
          followRedirect: true,
          maxRedirects: 10,
          headers: headers
        }, function(error, response, body) {
            console.log("error " +error + " body "+ body);
                if(!error ) console.log(response.statusCode);
                if (!error && response.statusCode == 200) {
                  if(body){    
                    var ob=JSON.parse(body);
                    res.send(body);
                  }
                }
                else if (!error){
                  console.log(" response.statusCode "+ response.statusCode);
                    res.status(response.statusCode).send(body);
                }
                else {
                    res.status(500).send(error);
                }    
        });
      }
      }