4

I have a request which processes thousands of data. So sometimes It takes more then 5 minutes to complete.

But unfortunately loopback returns timeout(no response from server) before process is completed.

In nodejs request. You can remove request timeout for specific request by below code.

request.setTimeout(0)

Can anyone tell me how can i do this for loopback remote method?

Ankur Akvaliya
  • 2,989
  • 4
  • 28
  • 53

2 Answers2

8

It was quite easy then it looked like.

All i had to do is pass http req object in my remote method and then set timeout to 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....
});

Thanks anyway!

Ankur Akvaliya
  • 2,989
  • 4
  • 28
  • 53
5

I am not quite sure of this but you can give it a try as i feel this might work

You can create a interceptor for that particular route or all the routes in the server.js file.

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

If not you can also use a remote method for that route and add the same there.

Update

If you want it for a single api just use

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();
});
Rahul Singh
  • 19,030
  • 11
  • 64
  • 86
  • Thanks for suggestion but i need ti for only one api. It should not affect any other. – Ankur Akvaliya Dec 11 '17 at 09:01
  • 1
    @AnkurAkvaliya then you can just go in the remote method of the same of best of in the `app.all('pass your api path')` this will work for that api only – Rahul Singh Dec 11 '17 at 09:16