2011-08-28 43 views
8

Tôi đã tự hỏi liệu có ai có thể cho tôi biết thời gian chờ yêu cầu HTTP mặc định là khi sử dụng express hay không.Thời gian yêu cầu HTTP Express.js

Điều tôi muốn nói là: sau bao nhiêu giây xử lý yêu cầu http, máy chủ Express/Node.js đóng kết nối, khi trình duyệt hoặc máy chủ đóng kết nối theo cách thủ công?

Làm cách nào để thay đổi thời gian chờ này cho một tuyến đường? Tôi muốn đặt khoảng 15 phút cho một tuyến chuyển đổi âm thanh đặc biệt.

Cảm ơn rất nhiều.

Tom

Trả lời

5

req.connection.setTimeout(ms); có thể là một ý tưởng tồi vì có thể gửi nhiều yêu cầu qua cùng một ổ cắm.

Hãy thử connect-timeout hoặc sử dụng này:

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

Cảm ơn, đã thay đổi câu trả lời của bạn thành câu trả lời được chấp nhận. – Tom

6

req.connection.setTimeout(ms); dường như đặt thời gian chờ yêu cầu cho một máy chủ HTTP trong Node.js.

+3

này đặt thời gian chờ kết nối không phải là thời gian chờ yêu cầu. – kilianc

Các vấn đề liên quan