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?
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. Basically with every res.write()
you add data to the body and send it with 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();