I am using a Mongoose cursor to retrieve a big amount of data in parts and send it progressively as I receive each part. To do this, I use the method res.write()
.
cursor.on('data', (doc) => {
/*Array opening bracket only sent at the start*/
if (total === 0) {
res.write('[');
}
docs.push(doc);
total += 1;
/*Limit indicates each CSV length*/
if (docs.length === limit) {
res.write(JSON.stringify(docs));
res.write(',');
docs = [];
}
});
cursor.on('end', (doc) => {
...
/*Closing bracket sent after the array is completed*/
res.write(']');
...
res.end();
});
As seen in the code above, I have a variable limit to know when to write the next part of the response. If this variable is too big, it requires more time to assemble each part, and the query finishes before I could write it. (If I reduce this variable, it works fine).
When this happens, the response only contains the string: '['
. As if it would have called the method res.end()
after a certain amount of time.
My question is: Is there a timeout when you use the res.write()
method, after which (if you don't keep writing or send the response) res.end()
is called? Or am I missing something?