node.js请求模块中的IP地址而不是域名

时间:2013-04-30 19:24:59

标签: node.js request ip

有没有办法在node.js请求模块中使用IP地址而不是域名?

我收到以下错误: [错误:无效的URI" 192.168.0.101/relay1/on"]

我知道我不应该包含网址方案(例如:http),所以http:// 192.168.0.101/relay1/on也不会工作。

以下是生成错误的代码:

  var arduinoRequestURL = arduinoIp + '/' + modulePrivateName + '/' + req.params.action;
  request(arduinoRequestURL, function (error, response, body) {
      console.log(body);
      console.log(error);
      console.log(response);
  });

以下是该模块的链接: https://github.com/mikeal/request

1 个答案:

答案 0 :(得分:0)

是的,你可以。您还必须指定请求协议。

request('google.com', function (error, response, body) {
      console.log(body);
      console.log(error);
      console.log(response);
});

给出[错误:无效的URI“google.com”]

以下是等效的:

request('http://google.com', function (error, response, body) {
      console.log(body);
      console.log(error);
      console.log(response);
});
request('http://74.125.236.18', function (error, response, body) {
      console.log(body);
      console.log(error);
      console.log(response);
});