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
|
@ -1,4 +1,6 @@
|
|||
const express = require('express');
|
||||
const fs = require('fs');
|
||||
const morgan = require('morgan');
|
||||
const bodyParser = require('body-parser');
|
||||
const sqlite3 = require('sqlite3').verbose();
|
||||
const app = express();
|
||||
|
@ -24,6 +26,23 @@ const db = new sqlite3.Database('db/results.db', (err) => {
|
|||
});
|
||||
|
||||
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
|
||||
app.get('/', (req, res) => {
|
||||
|
@ -37,7 +56,6 @@ app.get('/asset/frontend.js', (req, res) => {
|
|||
app.get('/getNextAnimal', async (req, res) => {
|
||||
try {
|
||||
// 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 randomAnimal = animals[randomIndex];
|
||||
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
|
||||
app.post('/recordButtonClick', (req, res) => {
|
||||
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) {
|
||||
console.error('Error recording button click:', err.message);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue