2015-08-30 12 views
5

Node.js Express'te büyük istekler gelebilecek bir API yapıyorum. İsteğin ne kadar büyük olduğunu görmek isterim.Bayt boyutu istek nasıl alınır?

//.... 
router.post('/apiendpoint', function(req, res, next) { 
    console.log("The size of incoming request in bytes is"); 
    console.log(req.????????????); //How to get this? 
}); 
//.... 
+3

'req.headers [' content-length '] 'yeterince iyi mi? – robertklep

+0

Awesome, bu gerçekten yeterli gibi görünüyor – Automatico

cevap

8

Sen req.socket.bytesRead kullanabilir veya request-stats modülünü kullanabilirsiniz.

var requestStats = require('request-stats'); 
var stats = requestStats(server); 

stats.on('complete', function (details) { 
    var size = details.req.bytes; 
}); 

ayrıntıları şöyle görünür nesne:

{ 
    ok: true,   // `true` if the connection was closed correctly and `false` otherwise 
    time: 0,   // The milliseconds it took to serve the request 
    req: { 
     bytes: 0,   // Number of bytes sent by the client 
     headers: { ... }, // The headers sent by the client 
     method: 'POST', // The HTTP method used by the client 
     path: '...'  // The path part of the request URL 
    }, 
    res : { 
     bytes: 0,   // Number of bytes sent back to the client 
     headers: { ... }, // The headers sent back to the client 
     status: 200  // The HTTP status code returned to the client 
    } 
} 

Yani details.req.bytes kadar isteği boyutu alabilirsiniz.

Başka bir seçenek req.headers['content-length']'dir (ancak bazı istemciler bu üstbilgiyi gönderemeyebilir).

+0

Çok güzel genel bakış. Ayrıca 'req.headers [' content-length ']' i de eklerseniz :) kabul edebilirim :) – Automatico

+1

Tamam, ama benim notuma dikkat et :) –

+0

Socket bytesRead işe yaramadı (genellikle çok büyük değerler veriyor), Bunun düğümün soket havuzundan kaynaklandığını varsayalım. İstek-istatistikleri güzel olsa da çalışır. –

İlgili konular