为什么expressjs在空闲时为PUT生成ERR_EMPTY_RESPONSE?

why does expressjs generate ERR_EMPTY_RESPONSE for PUT when idle?

目前

我在本地有一个快速服务器 运行 来管理来自 React 客户端的请求。

问题

当我的客户端空闲时(我假设之前客户端 PUT 服务器操作之后),大约 3-5 分钟后,控制台日志中会出现错误消息。

错误信息:

这会导致下一个客户端 PUT 到服务器失败,即数据未保存。

请求

我在中间件管理方面没有太多经验,因此希望能提供一些帮助来诊断可能导致此错误的原因。

可能有用的注释:

  1. 有时客户端对服务器的PUT次数过多,导致数据保存失败,但没有报错信息。我被迫重新加载页面。

客户端摘录 - App.js

  saveData = data => {
    console.log("Submitting request to save...");
    fetch('/api/v1/savedata', {
      method: 'PUT',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
    })
    .then(console.log("Save complete!"));
  };

摘自Server.js:

// An api endpoint that saves data from the client
app.put('/api/v1/savedata', (req,res) => {
  console.log('Server received request to saveData');
  let options = {
    files: './data/save.json',
    changes: req.body
  }
  updateJsonFile(options);
});

您没有在 API 中发送任何回复,请像这样使用 res.sendres.json

app.put('/api/v1/savedata', (req,res) => {
  console.log('Server received request to saveData');
  let options = {
    files: './data/save.json',
    changes: req.body
  }
  updateJsonFile(options);
  res.json({data: data}) // if you want to send json response
  // Note: please don't send response object more than once
});