-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsever.js
More file actions
66 lines (53 loc) · 1.81 KB
/
sever.js
File metadata and controls
66 lines (53 loc) · 1.81 KB
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
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const port = process.env.PORT || 3000;
// Static Files
app.use(express.static(path.join(__dirname, '/public')));
app.use(express.json());
// Food Data
const foodData = {
rice: { calories: 130, storage: 'roomTemperature' },
bread: { calories: 79, storage: 'roomTemperature' },
milk: { calories: 42, storage: 'refrigerator' },
eggs: { calories: 68, storage: 'refrigerator' },
cheese: { calories: 402, storage: 'refrigerator' },
chicken: { calories: 239, storage: 'refrigerator' },
};
app.post('/api/check-quality', (req, res) => {
const { foodName, expirationDate, storageCondition, appearance } = req.body;
const today = new Date();
const expDate = new Date(expirationDate);
const daysUntilExpiration = Math.ceil((expDate - today) / (1000 * 60 * 60 * 24));
let quality = 'Good';
if (daysUntilExpiration < 0) {
quality = 'Expired';
} else if (daysUntilExpiration < 3) {
quality = 'Poor';
} else if (daysUntilExpiration < 7) {
quality = 'Fair';
}
if (appearance < 5) {
quality = 'Poor';
}
const food = foodData[foodName.toLowerCase()];
if (!food) {
return res.status(400).json({ error: 'Unknown food item' });
}
if (storageCondition !== food.storage) {
quality = 'Poor';
}
res.json({
foodName,
quality,
calories: food.calories,
daysUntilExpiration,
recommendedStorage: food.storage,
});
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});