2011-08-28 20 views
8

मैं सोच रहा था कि कोई मुझे बता सकता है कि एक्सप्रेस का उपयोग करते समय डिफ़ॉल्ट HTTP अनुरोध टाइमआउट क्या है।Express.js HTTP अनुरोध टाइमआउट

मेरा मतलब यह है कि: http अनुरोध से निपटने के कितने सेकंड बाद एक्सप्रेस/Node.js सर्वर कनेक्शन बंद कर देगा, जब ब्राउज़र और न ही सर्वर मैन्युअल रूप से कनेक्शन बंद कर देता है?

मैं इस मार्ग को एक ही मार्ग के लिए कैसे बदलूं? मैं इसे एक विशेष ऑडियो रूपांतरण मार्ग के लिए लगभग 15 मिनट तक सेट करना चाहता हूं।

बहुत बहुत धन्यवाद।

टॉम

उत्तर

5

req.connection.setTimeout(ms); एक बुरा विचार हो सकता है क्योंकि एक ही सॉकेट पर एकाधिक अनुरोध भेजे जा सकते हैं।

connect-timeout का प्रयास करें या इस का उपयोग:

var errors = require('./errors'); 
const DEFAULT_TIMEOUT = 10000; 
const DEFAULT_UPLOAD_TIMEOUT = 2 * 60 * 1000; 

/* 
Throws an error after the specified request timeout elapses. 

Options include: 
    - timeout 
    - uploadTimeout 
    - errorPrototype (the type of Error to throw) 
*/ 
module.exports = function(options) { 
    //Set options 
    options = options || {}; 
    if(options.timeout == null) 
     options.timeout = DEFAULT_TIMEOUT; 
    if(options.uploadTimeout == null) 
     options.uploadTimeout = DEFAULT_UPLOAD_TIMEOUT; 
    return function(req, res, next) { 
     //timeout is the timeout timeout for this request 
     var tid, timeout = req.is('multipart/form-data') ? options.uploadTimeout : options.timeout; 
     //Add setTimeout and clearTimeout functions 
     req.setTimeout = function(newTimeout) { 
      if(newTimeout != null) 
       timeout = newTimeout; //Reset the timeout for this request 
      req.clearTimeout(); 
      tid = setTimeout(function() { 
       if(options.throwError && !res.finished) 
       { 
        //throw the error 
        var proto = options.error == null ? Error : options.error; 
        next(new proto("Timeout " + req.method + " " + req.url)); 
       } 
      }, timeout); 
     }; 
     req.clearTimeout = function() { 
      clearTimeout(tid); 
     }; 
     req.getTimeout = function() { 
      return timeout; 
     }; 
     //proxy end to clear the timeout 
     var oldEnd = res.end; 
     res.end = function() { 
      req.clearTimeout(); 
      res.end = oldEnd; 
      return res.end.apply(res, arguments); 
     } 
     //start the timer 
     req.setTimeout(); 
     next(); 
    }; 
} 
+0

धन्यवाद, आपके स्वीकृत उत्तर में बदल गया। – Tom

6

req.connection.setTimeout(ms); Node.js. में एक HTTP सर्वर के लिए अनुरोध का समय समाप्त सेट करने के लिए प्रकट होता है

+3

इस कनेक्शन समयबाह्य नहीं अनुरोध का समय समाप्त करता है। – kilianc

संबंधित मुद्दे