简体   繁体   中英

How prevent script from reloading when using hapi.js reply().hold()?

I've just started using hapi.js and have run into a problem when trying to defer the response for a route.

I successfully defer the response for the /query route using this code:

server.route([{
    method: 'GET',
    path: '/query',
    config: {
        handler: function(request, reply) {
            var response = reply().hold();              
            db = request.server.plugins['hapi-mongodb'].db;
            someFxn(callbackFxn, request, response);    
        }
    }

}]);

var someFxn(cb, req, res){
   var raw = {}; 
   //... do lots of stuff that can take a long time
   cb(req, res, raw); 
}

var callbackFxn = function(request, response, data){
    response.source = data; 
    response.send(); 
}

The problem I am having is that sometimes someFxn() takes a long time to execute and this seems to perhaps cause a timeout and the request is made again. How can I change the timeout value for the request so that it does not fire the request again?

I really appreciate any advice/leads anyone can give me.

You could be hitting the default Node.js socket timeout of 2 minutes. You can override it using the route config option config.timeout.socket :

From docs:

timeout - define timeouts for processing durations:

...

socket - by default, node sockets automatically timeout after 2 minutes. Use this option to override this behavior. Defaults to undefined which leaves the node default unchanged. Set to false to disable socket timeouts.

Set it like this to disable timeouts entirely for the route:

server.route([{
    method: 'GET',
    path: '/query',
    config: {
        timeout: {
            socket: false
        },
        handler: function(request, reply) {

            ...
        }
    }
}]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM