busybody
Your friendly neighborhood request tracker?
Example
Setup
;; const app = ;const tracker = ; app; app; app; app;
Output
API
busybody(options) -> tracker
Creates a new tracker. Takes in the following options.
step=21600000
- Time between steps in milliseconds. Defaults to 6 hours.window = 4
- Maximum steps to store.maxSize = 500
- Maximum unique paths to store per step, after path sanitization.precision = 2
- Maximum decimals to round to.preFilter(req, res) -> boolean
- Custom function to filter tracked requests before they're handled.postFilter(err, req, res) -> boolean
- Custom function to filter tracked requests after they're handled.sanitize(req, res) -> string
- Custom function to sanitize a url. By default, removes querystring, ids, uuids, and casing.onStep(step)
- shortcut fortracker.on('step', fn)
(see Events)onExpire(step)
- shortcut fortracker.on('expire', fn)
(see Events)
busybody.sanitize(req, res) -> string
The default sanitization function. It makes the following transformations to req.originalUrl
.
- Extract the pathname.
- Normalize the pathname.
- Replace all numbers with
:id
. - Replace all uuids with
:uuid
. - Make everything lowercase.
Example
> /path/../2/something?foo=bar
/:id/something/
> /foo/BAR/21/123e4567-e89b-12d3-a456-426655440000
/foo/bar/:id/:uuid/
tracker(req, res, next)
Express middleware that should be placed above routes you want to keep stats about.
tracker.getStats(sort = 'mean') -> object
Returns the stats output.
The sort
parameter can be count
, mean
, standardDeviation
, min
, or max
.
tracker.push(path, ms)
Manually track a path and duration.
Events
The middleware returned by busybody is also an EventEmitter
that exposes
the following events:
expire
- emitted when a step is about to expire. Passed the expiring step.step
- emitted after a new step was created. Passed the new step.