【发布时间】:2021-05-17 13:29:41
【问题描述】:
当写入次数较少(大约 10 次)时,响应结束。当写入次数超过 1000 时,则未达到 res.end() 并且请求处于挂起状态。有什么问题?
const pgp = require('pg-promise')();
const connection = {
host: 'localhost',
port: 5432,
database: 'mydb',
user: 'mydb_user',
password: 'abcdef'
};
const db = pgp(connection);
const handler = async (req, res, next) => {
res.set('Content-type', 'text/plain');
res.set('Transfer-Encoding', 'chunked');
for (let i = 0; i <= 500; i++) {
for (let j = 0; j <= 500; j++) {
let db_result = await db.any(`SELECT * FROM foo WHERE foo_id = ${i} AND bar_id = ${j};`);
res.cork();
res.write(JSON.stringify(db_result[0]));
process.nextTick(() => res.uncork());
}
}
res.end();
}
【问题讨论】:
标签: node.js async-await stream