gpt4 book ai didi

javascript - 在 Node js 中使用来自本地 apache 服务器的 http.request 提取数据失败

转载 作者:行者123 更新时间:2023-11-30 17:39:27 29 4
gpt4 key购买 nike

我有本地 Apache 'httpd' 服务器在我的机器上运行在端口 8123 上。

当我说 'telnet 127.0.0.1 8123' 时它有效,'curl 127.0.0.1:8123' 也有效。甚至 http 服务器也可以从浏览器运行。但是当我使用 Node.js 发出 http 请求时,它失败了。

我写的代码:-

var options = {
host: '127.0.0.1:8123',
path: '/test',
method: 'GET'
};
var callback = function(response) {
// Logs written here.
};
var req = http.request(options, callback);
req.on('error', function(e) { console.log('problem with request: ' + e.message); });
req.end()

当我将它作为“node test.js”执行时,它会抛出错误:- “请求问题:getaddrinfo ENOTFOUND”

我不明白我的 Node.js 无法解析到我自己的本地服务器。任何帮助表示赞赏。谢谢

最佳答案

您是否尝试过按照文档中的说明在您的选项中分离出端口? http://nodejs.org/api/http.html#http_http_request_options_callback

编辑:此外,使用 hostname 而不是 host 作为主机的 ip/domain 似乎是首选约定:

var options = {
hostname: '127.0.0.1',
port: 8123,
path: '/test',
method: 'GET'
};

关于javascript - 在 Node js 中使用来自本地 apache 服务器的 http.request 提取数据失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21381066/

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