
Product
Introducing Webhook Events for Alert Changes
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.
traffic-counter
Advanced tools
This express library is meant to count raw traffic (requests) on arbitrary express route handlers.
It is not meant to differentiate the values of query strings or parameters, but rather the expression specified in handlers.
As such, it is best used to determine traffic in core areas of your web page.
It's sole dependency is the node.js mongodb driver and a running database.
Unit tests are almost complete. Will need to add facility and tests to handle the database not being responsive gracefully (ie, not wait forever).
Also planning on adding an option to track urls, not just path expressions.
npm install traffic-counter
In the directory where the module is located, run the following 2 commands on the prompt:
Assuming you required the library in a TrafficCounter variable as follows...
var TrafficCounter = require('traffic-counter');
First, you need to ensure core collection dependencies by calling the following asynchronous call:
TrafficCounter.Setup(<Database>, <Callback>);
The argument, which is a mongodb database connection, is mandatory.
The argument is optional. If used, it takes the following form:
TrafficCounter.Setup(SomeDBConnection, function(Err) {
if(Err)
{
//Handle error
}
else
{
//Everything is fine, proceed...
}
});
Alternatively, you can put handlers on the following 2 events instead:
TrafficCounter.Event.SetupFinished
TrafficCounter.Event.Error
Taking the second route looks like this:
TrafficCounter.on(TrafficCounter.Event.SetupFinished, function() {
//Everything is fine, proceed
});
TrafficCounter.on(TrafficCounter.Event.Error, function(Err) {
//Handle error
});
TrafficCounter.Setup(SomeDBConnection);
Let's say you have the following paths for which you want to track traffic:
app.get('/', SomeFunction);
...
app.get('/Friends/:Friend', OtherFunction);
Assume that for the first one, you want to track the traffic per hour for the past 12 hours and for the second one, you want to track the traffic per day for the past 7 days.
You'd adjust your code as follow:
app.get('/', TrafficCounter.CountTraffic(TrafficCounter.TimeUnit.Hour, 12, app);
app.get('/', SomeFunction);
...
app.get('/', TrafficCounter.CountTraffic(TrafficCounter.TimeUnit.Day, 7, app);
app.get('/Friends/:Friend', OtherFunction);
The above will track traffic for the '/' and '/Friends/:Friend' paths. In the later case, requests to the URL /Friends/Peter and /Friends/Fred will count as the same path.
If you find '/' and '/Friends/:Friend' to be a little ugly, you can specify an alias for the paths as follow:
app.get('/', TrafficCounter.CountTraffic(TrafficCounter.TimeUnit.Hour, 12, app, 'MainPage'));
app.get('/', SomeFunction);
...
app.get('/Friends/:Friend', TrafficCounter.CountTraffic(TrafficCounter.TimeUnit.Day, 7, app, 'FriendLookup'));
app.get('/Friends/:Friend', OtherFunction);
More formally, TrafficCounter.CountTraffic has the following signature:
TrafficCounter.CountTraffic(<TimeUnit>, <Length>, <App>, <PathAlias>);
All arguments except the last are mandatory.
Due to incompleteness in the way Express preserves (or more specifically doesn't preserve) path expressions, I've been unable to find a way so far to preserve the path expression in the following 2 example cases:
App.use('/SomePath/:SomeParam', TrafficCounter.CountTraffic(..., App));
//Requests on /SomePath/1 will be stored as /SomePath/1, not /SomePath/:SomeParam
AppRouter = express.Router();
AppRouter.all('/SomePath', TrafficCounter.CountTraffic(..., AppRouter));
App.use('/SomePath/:SomeParam', AppRouter);
//Requests on /SomePath/1/SomePath will be stored as /SomePath/1/SomePath, not /SomePath/:SomeParam/SomePath
A workaround for this is to explicitly specify the path in the last parameter of TrafficCounter.CountTraffic:
App.use('/SomePath/:SomeParam', TrafficCounter.CountTraffic(..., App, '/SomePath/:SomeParam'));
//Now, requests on /SomePath/1 will be stored as /SomePath/:SomeParam
TrafficCounter.CountTraffic will make the following call when encountering an error:
next(<Error>)
As a result, it's important to put some error handling (handler with the (err, req, res, next) signature) in order to better control what the end user will see in his browser.
Additionally, the following event will be emited and can be caught:
TrafficCounter.Event.RequestError
There are 2 accessors to access the data. Both trigger the TrafficCounter.Event.ReportError event if a problem occurs:
GetPaths(<Callback>)Gets a list of paths monitored by the library so far.
takes the following form:
function(<Err>, <Paths>)
"Err" is an error if any occured (else its a falsy value) and "Paths" is an array containing the paths the library tracked so far as strings.
GetTraffic(<Params>, <Callback>)Gets either scalar or vectorial (array containing several tracked time intervals) information concerning the traffic for a given path.
This function takes a reference Date (defaults to the exact time when the function is called), computes a prior Date that is a given number of units of time before the reference Date and either sum all the traffic between those Dates (inclusive) or return the traffic for all recorded time intervals between those Dates.
"Params" is an object containing the following keys:
takes the following form:
function(<Err>, <Result>)
"Err" is an error if any occured (else its a falsy value).
"Result" takes the following form:
[<TimeInterval1>, <TimeInterval2>, ..., <TimeIntervalN>], where "TimeInterval..." takes the following form in turn:
{'Views': <NumberOfViews>, 'Date': <TruncatedDateRepresentingInterval>}The following method is used internally to operate on and truncate time:
TruncateTime(<TimeUnitParam>, <Time>, <Modifier>)
It is made available externally (ie, TrafficCounter.TrancateTime(...)) in case someone needs it to manipulate the "ReferenceTime" parameter for the "GetTraffic" method.
Assuming that you could do straightfoward arithmetic on Date objects, the operation it does would look like this:
Return = floor(<Time>+<Modifier>*<TimeUnitParam>, <TimeUnitParam>)
Ex:
var Now = new Date();
console.log(Now); //logs "Mon Dec 01 2014 06:01:11 GMT-0500 (EST)"
var Result = TrafficCounter.TruncateTime(TrafficCounter.TimeUnit.Hour, Now, 2);
console.log(Result); //logs "Mon Dec 01 2014 08:00:00 GMT-0500 (EST)"
FAQs
Express.js Middleware To Count Requests on Paths
We found that traffic-counter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.

Security News
ENISA has become a CVE Program Root, giving the EU a central authority for coordinating vulnerability reporting, disclosure, and cross-border response.

Product
Socket now scans OpenVSX extensions, giving teams early detection of risky behaviors, hidden capabilities, and supply chain threats in developer tools.