Res.json阻塞了我的node/express服务器

Res.json is blocking my node/express server

本文关键字:node express 服务器 我的 json Res      更新时间:2023-09-26

我有一个100mb的对象,我尝试用GET请求返回:

function doWork(req,res) {
  // creates a big object on a child process, then returns this big object
  res.json(bigObject); // <--- problem line
}
app.get('/dowork', doWork);

即使我在子进程上创建这个大对象,我需要主进程返回它(b/c子进程没有res流)。我想知道怎么做才是正确的?现在,将这个大对象转换为json占用了我的服务器一段时间。当它这样做时,没有人可以连接。

不要将其作为子进程的对象返回。相反,在那里调用JSON.stringify,并将流回主进程。然后,您可以通过使用.pipe()将子进程的输出流挂钩到res