is-critter/express/server.js
Vincent Batts 5c1b0ce6ff
express: thanks ChatGPT!
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2023-09-14 20:38:32 -04:00

40 lines
1 KiB
JavaScript

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
// Dummy database for storing button clicks
const buttonClicks = {
'Button 1': 0,
'Button 2': 0,
};
app.use(bodyParser.json());
// Serve the HTML file
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
// Route to get a random animal name
app.get('/getRandomAnimal', (req, res) => {
const animals = ['Dog', 'Cat', 'Elephant', 'Lion', 'Giraffe'];
const randomAnimal = animals[Math.floor(Math.random() * animals.length)];
res.json({ animalName: randomAnimal });
});
// Route to record button clicks
app.post('/recordButtonClick', (req, res) => {
const { buttonName } = req.body;
if (buttonClicks.hasOwnProperty(buttonName)) {
buttonClicks[buttonName]++;
res.sendStatus(200);
} else {
res.status(400).json({ error: 'Invalid button name' });
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});