$ npm install --save git+https://github.com/egeniesse/HitCount.git
var counter = require("hitcounter");
The counter object exposes properties to count hits using middleware, or manually managing the count.
How long to retain hit information (seconds)
Returns middleware that counts hits to the specified paths
An array of path strings for the counter to track. Each path passed in is automatically tagged with the path name by the counter. If nothing was passed in, it defaults to count each request as a hit, regardless of the endpoint.
Adds a hit to the count
Adds a hit to the tag count in addition to the total count
Returns the number of hits to api endpoints
Set the timeframe for hits to get back
Returns hits to endpoints that have the passed in tag
Clears out all of the stored hit records
var counter = require("hitcounter");
var express = require("express");
var app = express();
// initialize the middleware specifying the paths with config
app.use(counter.listen(["/app"]));
// set up a route to log hits
app.get("/app", function(req, res) {
res.send("This is a hit");
});
// set up a route that doesn't count towards the hit count
app.get("/data", function(req, res) {
res.send("This is not a hit");
});
// set up the endpoint we are designating to retrieve hit counts
app.get("/hits", function(req, res) {
var hits = counter.getHits(req.query.seconds);
res.send("There have been " + hits + " so far!");
});
// start the server
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
var counter = require("hitcounter");
var express = require("express");
var app = express();
// set up a route to log hits
app.get("/app", function(req, res) {
// manually add a hit when this path is called
counter.addHit()
res.send("This is a hit");
});
// set up the endpoint we are designating to retrieve hit counts
app.get("/hits", function(req, res) {
var hits = counter.getHits(req.query.seconds);
res.send("There have been " + hits + " so far!");
});
// start the server
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
var counter = require("hitcounter");
var express = require("express");
var app = express();
// set up the listener to listen to multiple endpoints
app.use(counter.listen(["/v1/app", "/v2/app"]));
app.get("/v1/app", function(req, res) {
res.send("This is a hit for /v1/app");
});
app.get("/v2/app", function(req, res) {
res.send("This is a hit for /v2/app");
});
// set up the endpoint we are designating to retrieve hit counts
app.get("/hits", function(req, res) {
// extract the tag and timeframe out of the query
var tag = req.query.tag;
var seconds = req.query.seconds;
var hits = counter.getHits(seconds, tag);
res.send("There have been " + hits + " so far!");
});
// start the server
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});