gpt4 book ai didi

node.js - 我的 http 请求有什么问题

转载 作者:可可西里 更新时间:2023-11-01 16:42:04 25 4
gpt4 key购买 nike

我可以在我的浏览器中输入我的 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 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)

我做错了什么?

最佳答案

从您显示的代码中可以看出的唯一错误是在您的 options 对象的 host 属性中包含了协议(protocol)方案。那应该只是:

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();

关于node.js - 我的 http 请求有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30306146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com