gpt4 book ai didi

node.js - 在node.js中使用http.request()在每个请求中获取请求超时

转载 作者:太空宇宙 更新时间:2023-11-03 23:44:48 28 4
gpt4 key购买 nike

我正在使用nodejs http.request方法将一些数据发布到外部API。我这样测试:

var options={
hostname:'www.google.com',
port:80,
method:'POST'
};

var req=http.request(options,function(res){
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});


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

});

这整个内容都包含在快速服务器发布路由中。但在控制台中它总是在一定时间后显示连接超时。我一无所知。

最佳答案

您只需创建一个request对象。你也必须发送它。使用 req.end() 实际发送请求。

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

关于node.js - 在node.js中使用http.request()在每个请求中获取请求超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15429527/

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