-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
67 lines (59 loc) · 2.05 KB
/
server.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const app = express();
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
const admins = require('./data/admins.json');
const foodItems = require('./data/foodItems.json');
// Admin login
app.post('/api/login', (req, res) => {
const { username, password } = req.body;
const admin = admins.find(admin => admin.username === username && admin.password === password);
if (admin) {
res.json({ message: 'Login successful', adminId: admin.id });
} else {
res.status(401).json({ message: 'Invalid credentials' });
}
});
// Change admin password
app.post('/api/change-password', (req, res) => {
const { id, newPassword } = req.body;
const admin = admins.find(admin => admin.id === id);
if (admin) {
admin.password = newPassword;
fs.writeFileSync('./data/admins.json', JSON.stringify(admins, null, 2));
res.json({ message: 'Password changed successfully' });
} else {
res.status(404).json({ message: 'Admin not found' });
}
});
// Get food items
app.get('/api/food-items', (req, res) => {
res.json(foodItems);
});
// Add food item
app.post('/api/food-items', (req, res) => {
const { name, price } = req.body;
const newItem = { id: Date.now(), name, price };
foodItems.push(newItem);
fs.writeFileSync('./data/foodItems.json', JSON.stringify(foodItems, null, 2));
res.json({ message: 'Food item added successfully' });
});
// Remove food item
app.delete('/api/food-items/:id', (req, res) => {
const { id } = req.params;
const index = foodItems.findIndex(item => item.id == id);
if (index !== -1) {
foodItems.splice(index, 1);
fs.writeFileSync('./data/foodItems.json', JSON.stringify(foodItems, null, 2));
res.json({ message: 'Food item removed successfully' });
} else {
res.status(404).json({ message: 'Food item not found' });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});