-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
37 lines (29 loc) · 937 Bytes
/
server.js
File metadata and controls
37 lines (29 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 3000;
app.use(express.json());
app.use(express.static('public'));
app.get('/images', (req, res) => {
const imagesDir = path.join(__dirname, 'public/images');
fs.readdir(imagesDir, (err, files) => {
if (err) {
return res.status(500).send('Error reading images directory');
}
const imageFiles = files.filter(file => /\.(jpg|jpeg|png|gif)$/.test(file));
res.json({ images: imageFiles });
});
});
app.post('/save-votes', (req, res) => {
const votes = req.body;
fs.writeFile(path.join(__dirname, 'votes.json'), JSON.stringify(votes, null, 2), (err) => {
if (err) {
return res.status(500).send('Error saving votes');
}
res.send({ status: 'Votes saved' });
});
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});