Node.js 我的http请求有什么问题
我可以在我的浏览器中输入我的url:192.168.1.132,我会得到“hello world”的正确响应,如果我做一个curl 192.168.1.132也是一样 但是如果我运行这个代码Node.js 我的http请求有什么问题,node.js,http,request,Node.js,Http,Request,我可以在我的浏览器中输入我的url:192.168.1.132,我会得到“hello world”的正确响应,如果我做一个curl 192.168.1.132也是一样 但是如果我运行这个代码 var http = require('http'); var options = { host: 'http://192.168.1.132', path: '/' }; callback = function(response) { var str = ''; //another
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)
我做错了什么?从您显示的代码中可以看出的唯一错误是在
选项
对象的主机
属性中包含了协议方案。这应该是:
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();
主机不应包含协议;如果没有协议,我会得到类似的错误事件未处理的“error”事件^error:Socket.socketOnData(_http_client.js:310:20)Socket.emit(events.js:107:17)readableAddChunk(_stream_readable.js:163:16)Socket.readable.push(_stream_readable.js:126:10)TCP.onread(net.js:529:20)trott)上的错误(本机)解析错误,我在没有协议的编辑主机上尝试了你的代码,我得到了一个“解析错误”,我的服务器正在运行nodeMCU,语句是“conn:on”(“Hello World”),输出是否需要以某种格式?