Package Exports
- request-stats
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (request-stats) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
request-stats
Get stats on your Node.js HTTP server requests.
Emits a stats event for each request with a single object as its first
argument, containing the following properties:
ok:trueif the connection was closed correctly andfalseotherwisetime: The milliseconds it took to serve the requestreq:bytes: Number of bytes sent by the clientheaders: The headers sent by the clientmethod: The HTTP method used by the clientpath: The path part of the request URL
res:bytes: Number of bytes sent back to the clientheaders: The headers sent back to the clientstatus: The HTTP status code returned to the client
Installation
npm install request-statsUsage
var requestStats = require('request-stats');
http.createServer(function (req, res) {
requestStats(req, res).on('stats', function (stats) {
console.log(stats); // { read: 42, written: 123, method: 'PUT', status: 200 }
});
});Or you can just parse it the http.Server object for a completely
decoupled experience:
var server = http.createServer(function (req, res) {
// ...
});
requestStats(server).on('stats', function (stats) {
console.log(stats); // { read: 42, written: 123, method: 'PUT', status: 200 }
});Alternative implementation
Instead of attaching the stats listener using the conventional .on() approach, you can also just parse the callback function as an optional extra argument:
var onStats = function (stats) {
// ...
};
// either inside the request callback:
requestStats(req, res, onStats);
// or with the entire server:
requestStats(server, onStats);Acknowledgement
Thanks to mafintosh for coming up with the initial concept and pointing me in the right direction.
License
MIT
