【问题标题】:(double proxy) node js create proxy server behind corporate proxy using node-http-proxy(双代理)node js 使用 node-http-proxy 在公司代理后面创建代理服务器
【发布时间】:2015-05-28 06:51:21
【问题描述】:

我有一个使用 node-http-proxy 创建代理来发送传入请求的应用程序,例如:http://localhost/api/login 到 https:// server1/api/login.这是使用的代码:

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',errno:'ENOTFOUND',系统调用:'getaddrinfo'}

问题是myserver和server1之间还有另一个代理(企业代理叫:localProxy)。

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

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

【问题讨论】:

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


    【解决方案1】:

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

    请在下面找到代码:

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

    【讨论】:

      猜你喜欢
      • 2013-05-30
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 2014-04-19
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多