【问题标题】:model.find({}) is not responding when I give the timeout as 5 minutes but it responds when the timeout is of 1 minute当我将超时设置为 5 分钟时,model.find({}) 没有响应,但是当超时为 1 分钟时它会响应
【发布时间】:2018-03-28 18:50:58
【问题描述】:

我正在研究 mongoose 以列出 mongodb 中集合中的所有数据,但我希望响应在 5 分钟后出现,但是当超时值为 5 分钟时它没有响应,但它在超时值为 1 分钟时响应

router.get(routeIdentifier+'/list/:id', function(req, res, next) {
        model.find({}, function (err, objects) {
            setTimeout(function(){
                if (err) return res.send(err);            
                objects.push({id:req.params.id})
                return res.json(objects);
            },300000)       
        });
     })

;

【问题讨论】:

  • 什么是 HTTP 服务器超时?
  • 我对此一无所知,我是nodejs的初学者
  • 为什么要延迟 5 分钟?
  • 其实它是一个测试项目,实际上我们需要打第三方api,这个api大约需要5分钟才能响应,所以我只是用setTimeout()测试
  • 这是个坏主意。如果需要更长的时间,您最好以异步模式转储响应。你绝不能占用 HTTP 服务器。

标签: node.js mongodb express mongoose mongoose-schema


【解决方案1】:

据我所知,这主要是由于 HTTP 服务器超时。在从数据库model 发送响应之前超时。

根据此处的文档,默认超时为 2 分钟:https://nodejs.org/dist/latest-v9.x/docs/api/http.html#http_server_settimeout_msecs_callback

请注意,ExpressJS 位于内置 NodeJS HTTP 服务器的顶部。

尝试以下方法:

    let server = http.createServer( expressApp );

    server.setTimeout( 300000 );

这会将 HTTP-Server 超时初始化为 5 分钟。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2011-12-12
    相关资源
    最近更新 更多