(双代理)节点js使用node-http-proxy在公司代理后面创建代理服务器

时间:2015-05-28 06:51:22

标签: node.js proxy double node-http-proxy corporate

我有使用node-http-proxy创建代理以发送传入请求的阳极应用程序,例如:http:// localhost / api / login to https:// server1的 / API /登录。这是使用的代码:

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

var proxy = httpProxy.createProxyServer();

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

这在我的机器上运行得很好。 现在,当我在服务器上部署它时,我收到错误:

{[错误:getaddrinfo ENOTFOUND]代码:' ENOTFOUND',错误:' ENOTFOUND',系统调用:' getaddrinfo' }

  

问题是myserver和server1之间还有另一个代理(名为localProxy的公司代理)。

我不知道在上面的代码中设置localProxy的位置。以及在哪里设置server1网址?

并且在这种情况下有没有办法使用node-http-proxy?

1 个答案:

答案 0 :(得分:0)

我的答案是停止使用代理模块并手动进行代理操作。

请找到以下代码:

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