【发布时间】: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.
}
})
【问题讨论】:
-
查看之前在 SO 上询问的how-can-i-use-an-http-proxy-with-node-js-http-client 的答案
-
我也有同样的问题 :(