【问题标题】:Loopback timeout(no response from server) when make a time consume request发出时间消耗请求时的环回超时(服务器无响应)
【发布时间】:2018-05-22 15:30:39
【问题描述】:

我有一个处理数千个数据的请求。所以有时需要超过 5 分钟才能完成。

但不幸的是,环回在进程完成之前返回超时(服务器没有响应)。

在 nodejs 请求中。您可以通过以下代码删除特定请求的请求超时。

request.setTimeout(0)

谁能告诉我如何为环回远程方法做到这一点?

【问题讨论】:

    标签: node.js loopbackjs strongloop loopback angular-loopback


    【解决方案1】:

    我不太确定,但你可以试一试,因为我觉得这可能有效

    您可以为该特定路由或server.js 文件中的所有路由创建拦截器。

    app.all('/api/*', function(req, res, next){
        request.setTimeout(0); // this is the statement and pass it to the next middle ware func.
        next();
    });
    

    如果没有,您也可以对该路由使用远程方法并添加 那里也一样。

    更新

    如果你想要它用于单个 api,只需使用

    app.all('<exact api path>', function(req, res, next){
        request.setTimeout(0); // this is the statement and pass it to the next middle ware func.
        next();
    });
    

    【讨论】:

    • 感谢您的建议,但我只需要一个 api。它不应该影响任何其他。
    • @AnkurAkvaliya 然后您可以使用app.all('pass your api path') 中最好的相同的远程方法,这仅适用于该api
    【解决方案2】:

    看起来很简单。

    我所要做的就是在我的远程方法中传递http req object,然后将超时设置为 0。

     Visit.remoteMethod(
            'caculateDistance',
            {
                description: 'Calculate distance from beacon using rssi',
                accepts: [
                    { arg: "req", type: "object", http: { source: "req" } },
                    { arg: 'activationId', type: 'string', required: true }
                returns: { arg: 'data', type: 'Object', root: true },
                http: { verb: 'patch', path: '/:activationId/calculate-distance' },
            }
        );
    
    
    Visit.caculateDistance = function (httpReq, activationId, callbackFn) {
            httpReq.setTimeout(0);
            /////calculations....
    });
    

    还是谢谢!

    【讨论】:

    • 没用......仍然在 50000 毫秒时崩溃!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    相关资源
    最近更新 更多