commit 0c0a5e2b0b8b5a16013678c72baa739889e18618
parent 120a3c4caa9447696236ba6786c94848982288d0
Author: Jake Bauer <jbauer@paritybit.ca>
Date: Sat, 30 Mar 2019 01:07:47 -0400
Add basic page hit counter to back end
Diffstat:
2 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/pages/home.md b/pages/home.md
@@ -8,8 +8,6 @@ Here is a list of what I plan to do so far:
* Enable IPv6 support (the domain has it, I just need to program the backend to
respond to IPv6 connections).
-* Add internal page hit counters so I can gauge the popularity of pages.
- connections to the webserver for analytical purposes.
* Add links/descriptions to all of my old and current projects.
* Add a page about me, which includes what I am a member of, what I value, and
a list of the organizations that I contribute to.
diff --git a/server/app.js b/server/app.js
@@ -1,7 +1,7 @@
"use strict";
/* @file app.js
* @author Jake Bauer
- * @date 2019-03-27
+ * @date 2019-03-30
* @original-date 2018-12-07
*
* @brief The code for the web server. Handles all HTTP/1.1 methods.
@@ -26,6 +26,16 @@ let http = require("http");
let path = require("path");
let crypto = require("crypto");
+let pageHits = {};
+
+function save_page_hits() {
+ fs.writeFile("./pageHits.json", JSON.stringify(pageHits), err => {
+ if (err) {
+ return console.log("Page Hit Write Error: " + err);
+ }
+ });
+}
+
function handle_error(err, res) {
// If the file cannot be found on the server
if (err.code === "ENOENT") {
@@ -90,6 +100,12 @@ const httpServer = http.createServer((req, res) => {
handle_error(err, res);
}
else {
+ if (pageHits[filePath] >= 1) {
+ pageHits[filePath] += 1;
+ }
+ else {
+ pageHits[filePath] = 1;
+ }
let etag = compute_etag(content);
// If the resource is cached, send code 304 and don't send resource
if (etag === req.headers["if-none-match"]) {
@@ -111,4 +127,5 @@ const httpServer = http.createServer((req, res) => {
});
}).listen(8080);
+setInterval(save_page_hits, 2000);
console.log("Server running.");