gpt4 book ai didi

node.js - NodeJS套接字挂起超时

转载 作者:太空宇宙 更新时间:2023-11-04 01:34:05 25 4
gpt4 key购买 nike

这本质上是长时间运行的进程(大约 30 分钟)的超时错误。

如何增加请求的超时时间?我已经尝试过 request.setTimeout(0, console.log); 但它对它没有任何影响。

var request = http.request({
hostname: 'localhost',
method: 'POST',
port: 3000,
headers: {
'accept': 'application/json;charset=UTF-8',
}
}, function(response){
var body;
var buffers = [];
response.on('data', buffers.push.bind(buffers));
response.on('end', function(){
try {
body = Buffer.concat(buffers).toString();
} catch(error) {console.error(body)}
});
}).on('error', function(error){ // errors caught in here
console.error("http error");
console.error(error);
});

request.setTimeout(0, console.log);

错误:

{ Error: socket hang up
at createHangUpError (_http_client.js:323:15)
at Socket.socketOnEnd (_http_client.js:426:23)
at Socket.emit (events.js:194:15)
at endReadableNT (_stream_readable.js:1103:12)
at process._tickCallback (internal/process/next_tick.js:63:19) code: 'ECONNRESET' }

编辑

enter image description here

屏幕截图2

罪魁祸首在服务器端是强大的,并且抛出中止错误

enter image description here

最佳答案

您需要在发出请求之前或发出请求时设置超时。

var request = http.request({
hostname: 'localhost',
method: 'POST',
port: 3000,
timeout: 0,
headers: {
'accept': 'application/json;charset=UTF-8',
}
}, function(response){
var body;
var buffers = [];
response.on('data', buffers.push.bind(buffers));
response.on('end', function(){
try {
body = Buffer.concat(buffers).toString();
} catch(error) {console.error(body)}
});
}).on('error', function(error){ // errors caught in here
console.error("http error");
console.error(error);
});

API 文档是您的 friend ;) https://nodejs.org/api/http.html#http_http_request_options_callback

关于node.js - NodeJS套接字挂起超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55112449/

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