【问题标题】:Node.js writable stream is hangingNode.js 可写流挂起
【发布时间】: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


    【解决方案1】:

    我想我找到了问题所在。在内部 for 循环中,我正在记录 db_result。看看here

    【讨论】:

      猜你喜欢
      • 2020-08-08
      • 2014-03-13
      • 2016-06-04
      • 2021-09-15
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 2019-03-22
      • 2012-02-20
      相关资源
      最近更新 更多