2011-08-28 187 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