-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver2.js
More file actions
62 lines (55 loc) · 1.79 KB
/
Copy pathserver2.js
File metadata and controls
62 lines (55 loc) · 1.79 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
const express = require('express');
const mysql = require('mysql');
const path = require('path');
const app = express();
const PORT = 8000;
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'nutrition_db',
port:'3306'
});
db.connect((err) => {
if (err) {
console.error('Database connection error:', err);
process.exit(1);
}
console.log('Connected to MySQL database');
});
app.use(express.static('docs'));
app.use(express.json());
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'docs', 'nutrition.html'));
});
// to handle nutrition calculation
app.get('/getNutrition', (req, res) => {
const foodName = req.query.food.toLowerCase();
const query = 'SELECT * FROM food_nutrition2 WHERE LOWER(name) = ?';
db.query(query, [foodName], (error, results) => {
if (error) {
console.error('Database query error:', error);
return res.json({ error: "Database query error" });
}
if (results.length > 0) {
const foodData = results[0];
res.json({
name: foodData.name,
Energy: foodData.Energy,
Protein: foodData.Protein,
Fat: foodData.Fat,
Carbos: foodData.Carbos,
Minerals: foodData.Minerals,
Fibre: foodData.Fibre,
Calcium: foodData.Calcium,
Phosphorous: foodData.Phosphorous,
Iron: foodData.Iron
});
} else {
res.json({ error: "Food item not found in the database." });
}
});
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});