【问题标题】:Node js late response issue(slow api responses)Node js 延迟响应问题(api 响应慢)
【发布时间】:2019-01-28 15:55:24
【问题描述】:

我有一个 REST API,当我的代码在多个集合字段中搜索时,它需要花费大量时间发送响应,而我的客户端需要该响应-side(更新列表) 我的问题是,在某个时间我从我的 API 得到了一个空白响应,即 在数据库中搜索之前发送了响应并得到一个空白响应作为结果

它确实搜索正确,但随着时间的推移,它无法正确响应。我该如何解决这个问题?

另外,我尝试发送response.end() at the end,但它不起作用

【问题讨论】:

  • 这可能是默认的超时时间。2分钟是节点服务器的默认值。
  • 你必须优化查询或设置 => res.setTimeout(0); 2 分钟后关闭 res
  • 好的,那我该怎么办..??我希望在我的客户端代码中得到响应..那么如何发送我的响应?
  • @PrashantGupta 你能在代码上显示这个吗??
  • 更新您的问题并发布您的查询......并尝试使用正在搜索的字段进行索引

标签: node.js mongodb mongoose


【解决方案1】:

如果这是其背后的原因,您可以禁用默认超时。

var http = require('http');
var server = http.createServer(function (req, res) {
  res.write('Hi!');
  res.end();
});
server.listen(8080);
console.log(server.timeout);
server.timeout = 0; //Set to 0 to disable any kind of automatic timeout behavior on incoming connections.
console.log(server.timeout);

【讨论】:

    【解决方案2】:

    此代码在 60 秒后停止响应,server.timeout 值以毫秒为单位。

    var server= http.createServer(app).listen(app.get('port'),app.get('host'), function(){
        server.timeout = 60000;
        //console.log(server.timeout);
      });
      console.log("Server listening at http://%s:%s",app.get('host'),app.get('port'));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-11
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 2020-09-19
      • 2022-06-10
      相关资源
      最近更新 更多