[英]I have an API that runs through an array and I want to send data to the client on each array element
I have an API that runs through an array and I want so send data to the client on each array element, i have tried using res.write with res.flush but it still waits until the end to send everything, is there a way that the data can be sent by chunks?我有一个 API 贯穿一个数组,我想在每个数组元素上向客户端发送数据,我尝试使用 res.write 和 res.flush 但它仍然等到最后发送所有内容,有没有办法数据可以分块发送吗?
for (let i = 0; i < searchUrl.length; i++) {
const element = searchUrl[i];
const element_details = await getDetailsforURL(element)
res.write(JSON.stringify(element_details))
res.flush()
//return_array.push(element_details)
}
res.end()
.write()
adds content to the body of the response. .write()
将内容添加到响应的正文中。 Basically with every res.write()
you add data to the body and send it with res.end()
.基本上每次
res.write()
都会将数据添加到正文并使用res.end()
发送。 If you want to send it in chunks you need one request per chunk, meaning that the client needs to continuously send requests until all data is received.如果要分块发送,则每个块需要一个请求,这意味着客户端需要不断发送请求,直到收到所有数据。
If you don't want to send the whole array, you could do the processing on the server side and send the result to the client:如果您不想发送整个数组,您可以在服务器端进行处理并将结果发送给客户端:
let response = [];
for (let i = 0; i < searchUrl.length; i++)
const element = searchUrl[i];
const element_details = await getDetailsforURL(element);
response.push(element_details);
}
res.write(JSON.stringify(element_details));
res.end();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.