发出时间消耗请求时的环回超时(服务器无响应)

Ank*_*iya 4 loopback node.js strongloop loopbackjs angular-loopback

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

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

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

request.setTimeout(0)
Run Code Online (Sandbox Code Playgroud)

谁能告诉我如何为环回远程方法执行此操作?

Ank*_*iya 7

看起来很容易。

我所要做的就是传入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....
});
Run Code Online (Sandbox Code Playgroud)

不管怎么说,还是要谢谢你!


Rah*_*ngh 5

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

您可以为该特定路由或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();
});
Run Code Online (Sandbox Code Playgroud)

如果没有,您也可以为该路由使用远程方法并在那里添加相同的方法。

更新

如果您想要单个 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();
});
Run Code Online (Sandbox Code Playgroud)