我正在尝试通过nodeJs中的代理进行连接,但没有任何结果或错误。
我正在使用 mikeal/request
。
我已经在命令行中测试了代理,它工作正常:
$ http_proxy=localhost:9060 wget http://wtfismyip.com/json
$ cat json
返回
{
"YourFuckingIPAddress" : "62.236.108.73",
"YourFuckingLocation" : "Finland",
"YourFuckingHostname" : "effi.org",
"YourFuckingISP" : "TDC Oy Finland"
}
正如预期的那样。但是我在nodeJs中的请求:
router.route('/proxy-ip')
.get(function (req, res) {
var request_options = {
url: 'http://wtfismyip.com/json',
proxy: {
host: "http://localhost",
port: 9060
}
};
console.log({request:request_options});
request.get(request_options,
function (error, response, json) {
if (!error && response.statusCode == 200) {
res.send(json);
} else {
console.log({'request': request, 'response': response, 'error': error, 'json': json});
res.send({'response': response, 'error': error, 'json': json});
}
}
);
});
记录无效协议(protocol)错误:
error: [Error: Invalid protocol: http]
有人知道怎么解决吗?有人在 mikeal/request 和 nodejs 中有使用代理的工作示例吗?
最佳答案
已修复!
我已经用字符串替换了代理对象并且它有效:
proxy: 'http://localhost:9060',
关于node.js - 在 NodeJS 中使用代理得到 "Invalid protocol",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26105047/