From 67ace14450b9753952ec49672c134d65740483be Mon Sep 17 00:00:00 2001 From: Cailean Finn Date: Thu, 20 Jun 2024 23:32:02 +0100 Subject: [PATCH] counter --- public/json/wanderers.json | 3 +++ server.js | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 public/json/wanderers.json diff --git a/public/json/wanderers.json b/public/json/wanderers.json new file mode 100644 index 0000000..23b2e5b --- /dev/null +++ b/public/json/wanderers.json @@ -0,0 +1,3 @@ +{ + "count": 0 +} \ No newline at end of file diff --git a/server.js b/server.js index 8d3a537..9efc8b3 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,6 @@ const express = require('express'); const path = require('path'); +const fs = require('fs') const bodyParser = require('body-parser'); const crypto = require('crypto'); const { exec } = require('child_process'); @@ -8,6 +9,17 @@ require('dotenv').config(); const app = express(); const PORT = process.env.PORT || 3000; const GITEA_SECRET = process.env.GITEA_SECRET +const counterFilePath = path.join(__dirname, 'public/json/wanderers.json'); + +function readCounter() { + const data = fs.readFileSync(counterFilePath) + return JSON.parse(data).count; +} + +function writeCounter(count) { + const data = JSON.stringify({ count: count}, null, 2) + fs.writeFileSync(counterFilePath, data) +} // Serve static files from the 'public' directory app.use(express.static(path.join(__dirname, 'public'))); @@ -23,7 +35,10 @@ app.use(bodyParser.json({ app.use((req, res, next) => { const urlPath = req.path.split('?')[0]; const htmlRoutes = ['/about', '/list', '/gallery']; - + let count = readCounter() + count++; + writeCounter(count) + console.log(count) if (htmlRoutes.includes(urlPath)) { req.url += '.html'; }