如何使用Node.js通过请求模块发送代理请求

时间:2019-02-25 10:39:55

标签: node.js request

这是我的代码:

  (function getComments(offset) {
    var options = {
      url: path + songId + '?limit=' + step + '&offset=' + offset,
      headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.8'
      },
      proxy: '***.***.***.***:****',
    };

    Request.get(options, function (error, res, body) {
      if (!error && res.statusCode === 200) {
        var data = JSON.parse(body);

        if (offset < data.total) {
          setTimeout(function () {
            console.log(offset);
            getComments(offset);
          }, Math.random() *2000 + 2000);
        } else {
          response.json(comments);
        }
      }
    });
  })(offset);

但是我的代理不起作用,使用Request.get()之类的message: "Invalid protocol: 125.123.143.186:"时出现错误

有人可以告诉我这是怎么发生的,我是否可以通过一种体面的方式发送代理请求?

1 个答案:

答案 0 :(得分:0)

在这种情况下,“协议无效”错误的最可能原因是您在选项对象中设置的URL看起来像这样:125.123.143.186:/some/path。检查您要查找的路径,我很确定它的格式错误,看起来您在IP地址后面没有端口。由于URL以协议和://的组合开头,因此Request的URL解析器将该IP地址视为协议,无法对其进行验证,然后显示此错误消息。