我正在使用Mongoose游标来检索部分中的大量数据,并在收到每个部分时逐步发送它。为此,我使用方法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();
});
如上面的代码所示,我有一个变量 limit ,知道何时编写响应的下一部分。如果此变量太大,则需要更多时间来组装每个部分,并且在我编写之前查询结束。 (如果我减少这个变量,它可以正常工作)。
发生这种情况时,响应只包含字符串:'['
。好像它会在一段时间后调用方法res.end()
。
我的问题是:当您使用res.write()
方法时是否超时(如果您不继续写或发送回复)res.end()
被调用?或者我错过了什么?
答案 0 :(得分:0)
res.write
没有超时。但是,如果您查看doc,虽然在第一次调用response.write
时隐式设置了标头,但它们可能不是您想要的
我会尝试在发送数据之前明确设置这些标头,即当您收到请求时:
response.setHeader('Connection', 'Transfer-Encoding');
response.setHeader('Content-Type', 'text/html; charset=utf-8');
response.setHeader('Transfer-Encoding', 'chunked');
我还会在response.write
添加一个简单的检查,以确保每个块都被刷新:
/*Limit indicates each CSV length*/
if (docs.length === limit) {
let chunk = JSON.stringify(docs);
if(res.write(`${chunk},`)) {
docs = [];
} else {
console.log(`oops! something is wrong with chunk ${chunk}`);
}
}

最后,我会阅读这个答案https://stackoverflow.com/a/16999405/3253893,以了解为什么如果您希望一次有多个查询,这可能是一个坏主意