Node.js 为什么express';sendFile函数是否将304视为错误?

Node.js 为什么express';sendFile函数是否将304视为错误?,node.js,express,http-status-code-304,Node.js,Express,Http Status Code 304,我有一个expressjs服务器,它使用sendFile函数将用户的文件发送到客户端。我使用函数的回调来知道何时发生错误。现在看来,每当sendFile决定响应304时(在我的例子中,这是理想的行为),它的回调就会被调用,并出现以下错误: Error: Request aborted: sendFile error at Object._onImmediate (.../node_modules/express/lib/response.js:941:17) at process

我有一个expressjs服务器,它使用sendFile函数将用户的文件发送到客户端。我使用函数的回调来知道何时发生错误。现在看来,每当sendFile决定响应304时(在我的例子中,这是理想的行为),它的回调就会被调用,并出现以下错误:

Error: Request aborted: sendFile error
    at Object._onImmediate (.../node_modules/express/lib/response.js:941:17)
    at processImmediate [as _immediateCallback] (timers.js:336:15)
 { [Error: Request aborted: sendFile error] code: 'ECONNABORT' }
然而,我找不到任何关于这一点的文档,我想知道如何确定何时忽略此错误以及何时对其作出反应,因为在其他情况下,此类错误可能是问题的症状,而不仅仅是预期行为的副作用


这真的是sendFile应该做的吗?有关于这方面的文件吗?如何将此错误与其他Econabort错误区分开来?

response.statusCode有助于检测此情况,但我不知道这是否也会捕获其他错误。这是我修改后的代码,改编自


我觉得这是对的。Express回复为304,文件尚未修改,因此连接被中止。发生错误的原因可能是试图通过中止的连接发送某些内容。如果文件自请求日期起未被修改,为什么它要发送除304以外的任何内容?参考:我无法在节点文档中看到此错误代码
ECONNABORT
。只有一个对
ECONNABORTED
的引用:也就是说,我只能在Express code中看到一个对
econnabortd
的单独引用:
res.sendFile(filename, options, function (err) {
  if (err) {
    if (err.code === "ECONNABORT" && res.statusCode == 304) {
      // No problem, 304 means client cache hit, so no data sent.
      console.log('304 cache hit for ' + filename);
      return;
    }
    console.error("SendFile error:", err, " (status: " + err.status + ")");
    if (err.status) {
      res.status(err.status).end();
    }
  }
  else {
    console.log('Sent:', filename);
  }