4

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

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

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

request.setTimeout(0)

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

4

2 回答 2

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....
});

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

于 2017-12-11T09:03:06.163 回答
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();
});

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

更新

如果您希望它用于单个 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();
});
于 2017-12-08T17:02:11.743 回答