【问题标题】:Simple example for nodejs behind a proxy代理后面的 nodejs 的简单示例
【发布时间】:2013-01-13 06:36:10
【问题描述】:

这是一个简单的 nodejs 脚本,用于使用 node 作为 https 客户端与外界交互。如何修改它,以便在 http://10.10.10.10:8080 这样的公司代理后面运行它时不会出现“ECONNREFUSED”错误?

var https = require('https');

var options = {
  hostname: 'encrypted.google.com',
  port: 443,
  path: '/',
  method: 'GET'
};

var req = https.request(options, function(res) {
  console.log("statusCode: ", res.statusCode);
  console.log("headers: ", res.headers);

  res.on('data', function(d) {
    process.stdout.write(d);
  });
});
req.end();

req.on('error', function(e) {
  console.error(e);
});

----更新---

我想出了如何使用 npm 'request' 包 (https://github.com/mikeal/request)。仍然想知道如何使用 vanilla 节点库来做到这一点。

var request = require('request').defaults({proxy:'http://my.proxy.server:8080', agent:false});
request('https://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Print the google web page.
  }
})

【问题讨论】:

标签: node.js https proxy


【解决方案1】:

通常最好在系统级别处理代理 - 将您的操作系统配置为使用代理而不是脚本。但是,这也可能会失败,因为大多数公司代理并未打开所有端口。

一个通用的解决方案是让服务器(EC2 或 Rackspace 实例)充当您自己的代理,并让它在端口 443 上侦听 ssh。端口 443 在公司防火墙中为 https 开放。

此时,您可以将所有本地网络流量通过隧道传输到服务器,或者通过 ssh 连接到服务器并在那里运行脚本。

如果您要制作特定于代理的脚本,它们的可移植性将不高,并且不会因为代理配置发生变化而破坏生产环境。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-13
    • 2010-11-06
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    相关资源
    最近更新 更多