-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
138 lines (108 loc) · 2.89 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const express=require('express');
const app=express();
const PORT=process.env.PORT
const path=require('path');
app.use(express.static(__dirname));
app.use(express.json());
let ingerdientnumber=4;
let dishesnumber=1;
const ingredientJson= [
{
id:1,
name:"brocooli",
imageurl:"https://st1.foodsd.co.il/Images/Products/large/uHmGri3t7b.jpg",
calories:34
}
,
{
id:2,
name:"tomato",
imageurl:"https://st1.foodsd.co.il/Images/Products/large/hagiSJ2GI3.jpg",
calories:40
} ,
{
id:3,
name:"soysauce",
imageurl:"https://cdn.shopify.com/s/files/1/0206/9470/products/10683_HFARM_49645309-3_grande.jpeg?v=1441105440",
calories:12
} ,
{
id:4,
name:"Nudels",
imageurl:"https://upload.wikimedia.org/wikipedia/commons/a/a8/Fresh_ramen_noodle_001.jpg",
calories:138
}
]
const Dishes= [
{
id:1,
dishname:"pad thai",
ingredients:[ingredientJson[0],ingredientJson[2],ingredientJson[3]],
time:20,
cookingmethod:"blabla",
imageurl:"https://d3o5sihylz93ps.cloudfront.net/wp-content/uploads/2020/05/13165500/IMG_0006-1.jpg"
}
]
app.get('',(req,res) => {
res.sendFile(path.join(__dirname,'/index.html'))
})
app.get('/init',(req,res) => {
res.send({
ingredients:ingredientJson,
dishes:Dishes,
numinger:ingerdientnumber,
numdishes:dishesnumber
})
})
app.post('/adddish',(req,res) => {
try {
const dish=req.body;
console.log(dish);
const dishingerconverted=[];
for(let i=0; i<dish.ingredients.length; i++){
dishingerconverted.push(ingredientJson.find(({id}) => id===dish.ingredients[i].id))
}
console.log('and this is dish converted:')
console.log(dishingerconverted)
Dishes.push({
id:dish.id,
dishname:dish.name,
ingredients:dishingerconverted,
time:dish.time,
cookingmethod:dish.cookingmethod,
imageurl:dish.imageurl
})
dishesnumber++;
res.status(200).send({
numdishes:dishesnumber,
ingerdient:ingredientJson,
dishes:Dishes
})
} catch(e) {
res.status(500).send();
}
})
app.post('/addingerdient',(req,res) => {
try{
const ingerdient=req.body;
console.log(req)
ingredientJson.push({
id:ingerdient.id,
name:ingerdient.name,
imageurl:ingerdient.imageurl,
calories:ingerdient.calories
})
ingerdientnumber++;
res.status(200).send({
ingerdient:ingredientJson,
dishes:Dishes,
numofingerdient:ingerdientnumber
});
}
catch(e) {
res.status(500).send();
}
})
app.listen(process.env.PORT, () => {
console.log('app on the air in port '+PORT+'');
})