node.js - 在 NodeJS 中使用代理得到 "Invalid protocol"

标签 node.js proxy

我正在尝试通过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/

相关文章:

proxy - iOS 13 Burp Suite 代理无法连接

python - Django 代理模型返回父模型

Nginx - 允许源 IP

node.js - hapi-auth-cookie 无法加载 session 策略

mysql - 忽略子字符串匹配的最高值的列

javascript - Node/Express API 路由中正确且可持续的错误处理方式

iphone - 以编程方式设置(系统范围)代理设置?

node.js - AngularJS + ExpressJS。代理 POST 请求待处理

html - WebRTC:将多个监听器连接到一个客户端,一次一个

json - NodeJS 包.json