是否使用Express中止先前的请求并响应最新的请求?

时间:2020-05-16 13:03:54

标签: node.js api express post settimeout

我有一个读取JSON并写入PDF和​​res.output('pdfFile')的API,超时时间为5分钟。

我的目的是在5分钟后发送响应,但是如果在这段时间内,有新请求发送到API,我想中止先前的请求并仅处理新请求。这是我到目前为止编写的代码。

app.post("/convert", (req, res) => {
  let body = req.body;
  let fileStream = fs.createWriteStream("files/output.pdf");

  const doc = new PDFDocument();
  doc.pipe(fileStream);

  res.setHeader("Content-type", "application/pdf");

  for (let question of body.questions) {
    doc.text(question);
  }

  doc.end();
  previousResponse = setTimeout(() => {
    res.download("files/output.pdf");
  }, 300000);
});

如何使用Node.js / Express做到这一点?

0 个答案:

没有答案