ethical view counting api
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sophie 9d783dbf41
hotfix for caddy
7 months ago
.gitignore implement ratelimiting 7 months ago
LICENSE Initial commit 7 months ago add web widget example 7 months ago
conf.json implement ratelimiting 7 months ago
index.js hotfix for caddy 7 months ago
package.json create viewcounter 7 months ago
viewcounter.service add example systemd service 7 months ago


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