You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1.9 KiB


ethical view counting api


embedded view counter

<div id="view-count"></div>
	var req = new XMLHttpRequest();
	req.addEventListener("load", function () {
			.innerHTML = "<b>page views</b> " + JSON.parse(this.responseText).views;
	});"POST", "/viewcounter");

requesting all view counts

sending a GET request will return a json object with all the stored viewcounts

$ curl localhost:8001/viewcounter

incrementing view counts/retrieving view count

to increment a view count, simply send a post request with the body containing the page path. the response will contain the view count

$ curl localhost:8001/viewcounter -d /index.html
{"code":200,"message":"counted page view","views":2}

if you have already incremented the view

$ curl localhost:8001/viewcounter -d /index.html
{"code":200,"message":"you have seen this page today","views":2}


	"port": 8001,
	"path": "/viewcounter",
	"site": "",
	"countDB": ".count.json",
	"ratelimitDB": ".ratelimit.json",
	"saveTimeout": 5000


port the service is hosted on


path to listen on

eg. a path of /api/viewcount would respond to http://ip:port/api/viewcount


site to validate urls against

eg. with a site of, posting /cheese.html will check if returns a 200 status code or not

countDB + ratelimitDB

filename for both the view counts and last viewed dates of pages attributed to ips

if you see your ratelimitDB getting absurdly large i'd recommend deleting it every once in a while


period to elapse before an update in view count is saved to disk, this saves the storage from constant strain from frequent changes