is-critter/express/server.js

41 lines
1 KiB
JavaScript
Raw Normal View History

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}`);
});