2023-09-15 00:38:32 +00:00
|
|
|
const express = require('express');
|
|
|
|
const bodyParser = require('body-parser');
|
2023-09-15 10:40:33 +00:00
|
|
|
const sqlite3 = require('sqlite3').verbose();
|
2023-09-15 00:38:32 +00:00
|
|
|
const app = express();
|
|
|
|
const port = 3000;
|
|
|
|
|
2023-09-15 10:40:33 +00:00
|
|
|
// Create an SQLite database and initialize tables
|
|
|
|
const db = new sqlite3.Database('mydb.db', (err) => {
|
2023-09-15 01:35:41 +00:00
|
|
|
if (err) {
|
2023-09-15 10:40:33 +00:00
|
|
|
console.error('Error opening SQLite database:', err.message);
|
|
|
|
} else {
|
|
|
|
console.log('Connected to SQLite database');
|
|
|
|
db.run(`
|
|
|
|
CREATE TABLE IF NOT EXISTS button_clicks (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
session_id TEXT,
|
|
|
|
button_name TEXT,
|
|
|
|
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
time_difference INTEGER -- Add this column for time difference
|
|
|
|
)
|
|
|
|
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
});
|
2023-09-15 01:35:41 +00:00
|
|
|
|
2023-09-15 10:40:33 +00:00
|
|
|
app.use(bodyParser.json());
|
2023-09-15 01:35:41 +00:00
|
|
|
|
2023-09-15 00:38:32 +00:00
|
|
|
// Serve the HTML file
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
res.sendFile(__dirname + '/index.html');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Route to get a random animal name
|
2023-09-15 10:40:33 +00:00
|
|
|
app.get('/getRandomAnimal', async (req, res) => {
|
|
|
|
try {
|
|
|
|
const animals = ['Dog', 'Cat', 'Elephant', 'Lion', 'Giraffe'];
|
|
|
|
const randomIndex = Math.floor(Math.random() * animals.length);
|
|
|
|
const randomAnimal = animals[randomIndex];
|
|
|
|
res.json({ animalName: randomAnimal });
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error fetching random animal:', error);
|
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
|
|
}
|
2023-09-15 00:38:32 +00:00
|
|
|
});
|
|
|
|
|
2023-09-15 10:40:33 +00:00
|
|
|
// Route to record button clicks along with session IDs in SQLite
|
2023-09-15 00:38:32 +00:00
|
|
|
app.post('/recordButtonClick', (req, res) => {
|
2023-09-15 10:40:33 +00:00
|
|
|
try {
|
|
|
|
const { buttonName, sessionId } = req.body;
|
|
|
|
|
|
|
|
db.run('INSERT INTO button_clicks (session_id, button_name) VALUES (?, ?)', [sessionId, buttonName], (err) => {
|
|
|
|
if (err) {
|
|
|
|
console.error('Error recording button click:', err.message);
|
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
|
|
} else {
|
|
|
|
res.sendStatus(200);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error recording button click:', error);
|
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
2023-09-15 00:38:32 +00:00
|
|
|
}
|
2023-09-15 01:35:41 +00:00
|
|
|
});
|
|
|
|
|
2023-09-15 10:40:33 +00:00
|
|
|
// Route to show the current results from SQLite
|
|
|
|
app.get('/results', (req, res) => {
|
2023-09-15 01:35:41 +00:00
|
|
|
try {
|
2023-09-15 10:40:33 +00:00
|
|
|
db.all('SELECT button_name, COUNT(*) as count FROM button_clicks GROUP BY button_name', (err, rows) => {
|
|
|
|
if (err) {
|
|
|
|
console.error('Error fetching results:', err.message);
|
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
|
|
} else {
|
|
|
|
const results = {};
|
|
|
|
rows.forEach((row) => {
|
|
|
|
results[row.button_name] = row.count;
|
|
|
|
});
|
|
|
|
res.json(results);
|
|
|
|
}
|
|
|
|
});
|
2023-09-15 01:35:41 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Error fetching results:', error);
|
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
|
|
}
|
2023-09-15 00:38:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Server is running on port ${port}`);
|
|
|
|
});
|
|
|
|
|