express: thanks ChatGPT!
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
3eb17c1935
commit
5c1b0ce6ff
4 changed files with 757 additions and 0 deletions
40
express/server.js
Normal file
40
express/server.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
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}`);
|
||||
});
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue