I am making an API in Node.js Express which might get large requests coming in. I would really like to see how big the request are.
//....
router.post('/apiendpoint', function(req, res, next) {
console.log("The size of incoming request in bytes is");
console.log(req.????????????); //How to get this?
});
//....
4. There should be between 54 to 66 bytes from the very start of the Ethernet frame to the ASCII “G” in the http “GET”.
To check this Content-Length in action go to Inspect Element -> Network check the request header for Content-Length like below, Content-Length is highlighted. Supported Browsers: The browsers compatible with HTTP headers Content-length are listed below: Google Chrome. Internet Explorer.
You can use req. socket. bytesRead or you can use the request-stats module. So you can get the request size from details.
GET requests are used to send only limited amount of data because data is sent into header while POST requests are used to send large amount of data because data is sent in the body. Express. js facilitates you to handle GET and POST requests using the instance of express.
You can use req.socket.bytesRead
or you can use the request-stats module.
var requestStats = require('request-stats');
var stats = requestStats(server);
stats.on('complete', function (details) {
var size = details.req.bytes;
});
The details object looks like this:
{
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
}
}
So you can get the request size from details.req.bytes
.
Another option is req.headers['content-length']
(but some clients might not send this header).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With