2015-05-18 75 views
2

我能进入我的网址:192.168.1.132到我的浏览器,我得到的“Hello World”,同样的事情,正确的响应,如果我做的卷曲192.168.1.132有什么不对我的http请求

但如果我运行此代码

var http = require('http'); 

var options = { 
    host: 'http://192.168.1.132', 
    path: '/' 
}; 

callback = function(response) { 
    var str = ''; 

    //another chunk of data has been recieved, so append it to `str` 
    response.on('data', function (chunk) { 
    str += chunk; 
    }); 

    //the whole response has been recieved, so we just print it out here 
    response.on('end', function() { 
    console.log(str); 

我得到错误:

events.js:85 
     throw er; // Unhandled 'error' event 
      ^
Error: getaddrinfo ENOTFOUND http://192.168.1.132 
    at errnoException (dns.js:44:10) 
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:94:26) 

我在做什么错?

+0

主机不应该包含协议;只是'192.168.1.132' –

+0

没有协议,我得到类似的错误events.js:85 throw er; //未处理 '错误' 事件 ^ 错误:解析错误 在错误(天然) 在Socket.socketOnData(_http_client.js:310:20) 在Socket.emit(events.js:107:17) 在在TCP.onread(net.js:529:20)下使用Socket.Readable.push(_stream_readable.js:126:10)可读的AddAddunk(_stream_readable.js:163:16) – user1117057

回答

0

从您显示的代码中唯一可以识别的错误是在您的options对象的host属性中包含协议方案。这应该是:

host: '192.168.1.132', 

该更改后发生的任何进一步的错误是在您没有显示的代码中。以下是基于代码的工作代码示例。

var http = require('http'); 

var options = { 
    host: 'www.google.com', 
    path: '/index.html' 
}; 

callback = function(response) { 
    var str = ''; 

    //another chunk of data has been recieved, so append it to `str` 
    response.on('data', function (chunk) { 
    str += chunk; 
    }); 

    //the whole response has been recieved, so we just print it out here 
    response.on('end', function() { 
    console.log(str); 
    }); 
}; 

var req = http.request(options, callback); 

req.on('error', function (e) { 
    console.log('problem with request: ' + e.message); 
}); 

req.end(); 
+0

trott,我试着用编辑好的主机没有协议,我得到一个“解析错误”我的服务器正在运行nodeMCU与语句“conn:on(”

Hello World

“)输出是否需要在某种格式? – user1117057