express: ready for voting, but the /results display is not good
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
e3150bd53b
commit
5c4e28e2de
4 changed files with 83 additions and 5 deletions
|
@ -30,7 +30,10 @@ function getSessionId() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function clearSessionId() {
|
function clearSessionId() {
|
||||||
document.cookie = ``;
|
const newSessionId = generateSessionId();
|
||||||
|
document.cookie = `sessionId=${newSessionId}`;
|
||||||
|
setSessionStartTime();
|
||||||
|
getSessionId();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to set session start time
|
// Function to set session start time
|
||||||
|
@ -58,6 +61,7 @@ document.getElementById('isNotCritterButton').addEventListener('click', () => {
|
||||||
|
|
||||||
document.getElementById('startOverButton').addEventListener('click', () => {
|
document.getElementById('startOverButton').addEventListener('click', () => {
|
||||||
clearSessionId();
|
clearSessionId();
|
||||||
|
getNextAnimal();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Function to record button clicks on the server
|
// Function to record button clicks on the server
|
||||||
|
@ -68,12 +72,14 @@ async function recordButtonClick(buttonName, sessionId) {
|
||||||
const timeDifference = currentTime - lastButtonClickTime;
|
const timeDifference = currentTime - lastButtonClickTime;
|
||||||
// Include the time difference in the POST request data
|
// Include the time difference in the POST request data
|
||||||
const animal = document.getElementById('animal-name').textContent;
|
const animal = document.getElementById('animal-name').textContent;
|
||||||
|
const bodyData = JSON.stringify({ "animal": animal, "button": buttonName, "session": sessionId, "difference": timeDifference, "time": sessionStartTime });
|
||||||
|
console.log(bodyData);
|
||||||
await fetch('/recordButtonClick', {
|
await fetch('/recordButtonClick', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ "animal": animal, "button": buttonName, "session": sessionId, "difference": timeDifference, "time": sessionStartTime }),
|
body: bodyData,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
lastButtonClickTime = currentTime; // Record the timestamp of the button click
|
lastButtonClickTime = currentTime; // Record the timestamp of the button click
|
||||||
|
|
51
express/package-lock.json
generated
51
express/package-lock.json
generated
|
@ -11,6 +11,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.20.2",
|
"body-parser": "^1.20.2",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
|
"morgan": "^1.10.0",
|
||||||
"sqlite3": "^5.1.6"
|
"sqlite3": "^5.1.6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -181,6 +182,22 @@
|
||||||
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
|
||||||
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="
|
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="
|
||||||
},
|
},
|
||||||
|
"node_modules/basic-auth": {
|
||||||
|
"version": "2.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz",
|
||||||
|
"integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==",
|
||||||
|
"dependencies": {
|
||||||
|
"safe-buffer": "5.1.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/basic-auth/node_modules/safe-buffer": {
|
||||||
|
"version": "5.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
||||||
|
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
|
||||||
|
},
|
||||||
"node_modules/body-parser": {
|
"node_modules/body-parser": {
|
||||||
"version": "1.20.2",
|
"version": "1.20.2",
|
||||||
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz",
|
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz",
|
||||||
|
@ -1066,6 +1083,32 @@
|
||||||
"node": ">=10"
|
"node": ">=10"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/morgan": {
|
||||||
|
"version": "1.10.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz",
|
||||||
|
"integrity": "sha512-AbegBVI4sh6El+1gNwvD5YIck7nSA36weD7xvIxG4in80j/UoK8AEGaWnnz8v1GxonMCltmlNs5ZKbGvl9b1XQ==",
|
||||||
|
"dependencies": {
|
||||||
|
"basic-auth": "~2.0.1",
|
||||||
|
"debug": "2.6.9",
|
||||||
|
"depd": "~2.0.0",
|
||||||
|
"on-finished": "~2.3.0",
|
||||||
|
"on-headers": "~1.0.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.8.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/morgan/node_modules/on-finished": {
|
||||||
|
"version": "2.3.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",
|
||||||
|
"integrity": "sha512-ikqdkGAAyf/X/gPhXGvfgAytDZtDbr+bkNUJ0N9h5MI/dmdgCs3l6hoHrcUv41sRKew3jIwrp4qQDXiK99Utww==",
|
||||||
|
"dependencies": {
|
||||||
|
"ee-first": "1.1.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/ms": {
|
"node_modules/ms": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||||
|
@ -1226,6 +1269,14 @@
|
||||||
"node": ">= 0.8"
|
"node": ">= 0.8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/on-headers": {
|
||||||
|
"version": "1.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz",
|
||||||
|
"integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/once": {
|
"node_modules/once": {
|
||||||
"version": "1.4.0",
|
"version": "1.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.20.2",
|
"body-parser": "^1.20.2",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
|
"morgan": "^1.10.0",
|
||||||
"sqlite3": "^5.1.6"
|
"sqlite3": "^5.1.6"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
|
const fs = require('fs');
|
||||||
|
const morgan = require('morgan');
|
||||||
const bodyParser = require('body-parser');
|
const bodyParser = require('body-parser');
|
||||||
const sqlite3 = require('sqlite3').verbose();
|
const sqlite3 = require('sqlite3').verbose();
|
||||||
const app = express();
|
const app = express();
|
||||||
|
@ -24,6 +26,23 @@ const db = new sqlite3.Database('db/results.db', (err) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
app.use(morgan('combined'));
|
||||||
|
|
||||||
|
var animals;
|
||||||
|
// check and load animals into redis
|
||||||
|
try {
|
||||||
|
fs.readFile("./animals.json", function (err, data) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
var jsondata = JSON.parse(data);
|
||||||
|
animals = jsondata.animals;
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error loading animals:', error);
|
||||||
|
animals = ['Dog', 'Cat', 'Elephant', 'Lion', 'Giraffe'];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Serve the HTML file
|
// Serve the HTML file
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
|
@ -37,7 +56,6 @@ app.get('/asset/frontend.js', (req, res) => {
|
||||||
app.get('/getNextAnimal', async (req, res) => {
|
app.get('/getNextAnimal', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
// TODO this is currently random, and should have a bit of reasoning behind the next choice
|
// TODO this is currently random, and should have a bit of reasoning behind the next choice
|
||||||
const animals = ['Dog', 'Cat', 'Elephant', 'Lion', 'Giraffe'];
|
|
||||||
const randomIndex = Math.floor(Math.random() * animals.length);
|
const randomIndex = Math.floor(Math.random() * animals.length);
|
||||||
const randomAnimal = animals[randomIndex];
|
const randomAnimal = animals[randomIndex];
|
||||||
res.json({ animalName: randomAnimal });
|
res.json({ animalName: randomAnimal });
|
||||||
|
@ -50,9 +68,11 @@ app.get('/getNextAnimal', async (req, res) => {
|
||||||
// Route to record button clicks along with session IDs in SQLite
|
// Route to record button clicks along with session IDs in SQLite
|
||||||
app.post('/recordButtonClick', (req, res) => {
|
app.post('/recordButtonClick', (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { buttonName, sessionId } = req.body;
|
//const { buttonName, sessionId } = req.body;
|
||||||
|
const result = req.body;
|
||||||
|
console.error(result);
|
||||||
|
|
||||||
db.run('INSERT INTO button_clicks (session_id, button_name) VALUES (?, ?)', [sessionId, buttonName], (err) => {
|
db.run('INSERT INTO button_clicks (session_id, animal_name, button_name, timestamp, time_difference) VALUES (?, ?, ?, ?, ?)', [result.session, result.animal, result.button, result.time, result.difference], (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('Error recording button click:', err.message);
|
console.error('Error recording button click:', err.message);
|
||||||
res.status(500).json({ error: 'Internal server error' });
|
res.status(500).json({ error: 'Internal server error' });
|
||||||
|
|
Loading…
Reference in a new issue