1
- // const dotenv = require("dotenv");
2
- // dotenv.config({ path: "./config.env" });
1
+ // Imports from libraries
3
2
const express = require ( "express" ) ;
4
3
const bodyParser = require ( "body-parser" ) ;
5
- const striptags = require ( "striptags" ) ;
6
- const cheerio = require ( "cheerio" ) ;
7
-
8
4
const ejs = require ( "ejs" ) ;
9
5
6
+ // Imports from projects
7
+ const viewsRoutes = require ( "./routes/viewsRoutes" ) ;
8
+ const weekMenusRoutes = require ( "./routes/weekMenusRoutes" ) ;
9
+ const fridgeRoutes = require ( "./routes/fridgeRoutes" ) ;
10
+ const tagRoutes = require ( "./routes/tagRoutes" ) ;
11
+ const recipeRoutes = require ( "./routes/recipeRoutes" ) ;
12
+
10
13
const app = express ( ) ;
11
14
12
- app . set ( "view engine" , "ejs" ) ;
15
+ // Req body modifiers
13
16
app . use ( bodyParser . urlencoded ( { extended : true } ) ) ;
14
- app . use ( express . static ( "public" ) ) ;
15
-
16
- const apiKey = "54fe1ed340324dde9577a8ba35e809fc" ;
17
-
18
- // All the get request to render different page.
19
-
20
- app . get ( "/" , ( req , res ) => {
21
- res . render ( "home" ) ;
22
- } ) ;
23
-
24
- app . get ( "/inputWeekPlanMeals" , ( req , res ) => {
25
- res . render ( "inputWeekPlanMeals" ) ;
26
- } ) ;
27
-
28
- app . get ( "/inputIngredient" , ( req , res ) => {
29
- res . render ( "inputIngredient" ) ;
30
- } ) ;
31
-
32
- app . get ( "/inputRandom" , ( req , res ) => {
33
- res . render ( "inputRandom" ) ;
34
- } ) ;
35
-
36
- // POST request to render menus for a week based on user input
37
-
38
- app . post ( "/mealList" , async ( req , res ) => {
39
- // Variables for meal planning based on user input
40
- const targetCalories = req . body . targetCalories ;
41
- let diets = req . body . diets ;
42
- if ( typeof diets === "string" ) {
43
- diets = req . body . diets . split ( "," ) ;
44
- }
45
- let cuisines = req . body . cusines ;
46
- if ( typeof cuisines === "string" ) {
47
- cuisines = req . body . cuisines . split ( "," ) ;
48
- }
49
- let excludeIngredients = req . body . excludeIngredients ;
50
- if ( typeof excludeIngredients === "string" ) {
51
- excludeIngredients = req . body . excludeIngredients . split ( "," ) ;
52
- }
53
- let includeIngredients = req . body . includeIngredients ;
54
- if ( typeof includeIngredients === "string" ) {
55
- includeIngredients = req . body . includeIngredients . split ( "," ) ;
56
- }
57
- let preferences = req . body . preferences ;
58
- if ( typeof preferences === "string" ) {
59
- preferences = req . body . preferences . split ( "," ) ;
60
- }
61
-
62
- try {
63
- const { default : fetch } = await import ( "node-fetch" ) ;
64
-
65
- // Week meal plan using user input
66
- const weekPlan = await fetch (
67
- `https://api.spoonacular.com/mealplanner/generate?apiKey=${ apiKey } &timeFrame=week&targetCalories=${ targetCalories } &diet=${ diets } &excludeIngredients=${ excludeIngredients } &includeIngredients=${ includeIngredients } &cuisine=${ cuisines } &preferences=${ preferences } `
68
- ) ;
69
-
70
- const dataOfMeal = await weekPlan . json ( ) ;
71
- const days = await dataOfMeal . week ;
72
-
73
- res . render ( "planMeals" , { meals : days } ) ;
74
- } catch ( error ) {
75
- res . send ( "Sorry! Some error occurred. Please try again!" ) ;
76
- console . log ( error ) ;
77
- }
78
- } ) ;
79
-
80
- //POST request to generate recipe based on ingredients provided by user
81
-
82
- app . post ( "/ingredientList" , async ( req , res ) => {
83
- // Storing the input provided by user in a variable in backend
84
-
85
- let ingredients = req . body . ingredients ;
86
- if ( typeof ingredients === "string" ) {
87
- ingredients = req . body . ingredients . split ( "," ) ;
88
- }
89
-
90
- let number = req . body . number ;
91
- if ( typeof number === "string" ) {
92
- number = req . body . number . split ( "," ) ;
93
- }
94
-
95
- try {
96
- const { default : fetch } = await import ( "node-fetch" ) ;
97
17
98
- //Generate recipes by ingredients
99
- const generateRecipesByIngredients = await fetch (
100
- `https://api.spoonacular.com/recipes/findByIngredients?ingredients=${ ingredients } &number=${ number } &apiKey=${ apiKey } `
101
- ) ;
102
- const recipesData = await generateRecipesByIngredients . json ( ) ;
103
-
104
- // Generate recipe information
105
- const recipeInfoPromises = recipesData . map ( async ( recipe ) => {
106
- const recipeInfo = await fetch (
107
- `https://api.spoonacular.com/recipes/${ recipe . id } /nutritionWidget.json?apiKey=${ apiKey } `
108
- ) ;
109
- const infoData = await recipeInfo . json ( ) ;
110
- return {
111
- ...recipe ,
112
- nutrients : infoData ,
113
- } ;
114
- } ) ;
115
-
116
- // Wait for all recipe nutrient information to be fetched
117
- const recipesWithNutrients = await Promise . all ( recipeInfoPromises ) ;
118
-
119
- res . render ( "planMealsByIngredients" , {
120
- recipes : recipesWithNutrients ,
121
- } ) ;
122
-
123
- // res.send(recipesWithNutrients);
124
- } catch ( error ) {
125
- res . send ( "Sorry! Some error occurred. Please try again!" ) ;
126
- console . log ( error ) ;
127
- }
128
- } ) ;
129
-
130
- //POST request to generate random list of recipes based on tags provided by user
131
-
132
- app . post ( "/randomList" , async ( req , res ) => {
133
- // Tags and number provided by user
134
-
135
- const number = req . body . number ;
136
- let tags = req . body . tags ;
137
- if ( typeof tags === "strings" ) {
138
- tags = req . body . tags . split ( "," ) ;
139
- }
140
-
141
- try {
142
- const { default : fetch } = await import ( "node-fetch" ) ;
143
- const generateRandomRecipes =
144
- await fetch ( `https://api.spoonacular.com/recipes/random?number=${ number } &tags=${ tags } &apiKey=${ apiKey }
145
- ` ) ;
146
-
147
- const randomData = await generateRandomRecipes . json ( ) ;
148
-
149
- // Generate Recipe information including nutrients
150
-
151
- const recipeInfoPromises = randomData . recipes . map ( async ( recipe ) => {
152
- const recipeInfo = await fetch (
153
- `https://api.spoonacular.com/recipes/${ recipe . id } /nutritionWidget.json?apiKey=${ apiKey } `
154
- ) ;
155
- const infoData = await recipeInfo . json ( ) ;
156
- return {
157
- ...recipe ,
158
- nutrients : infoData ,
159
- } ;
160
- } ) ;
161
-
162
- // Wait for all recipe nutrient information to be fetched
163
- const recipesWithNutrients = await Promise . all ( recipeInfoPromises ) ;
164
-
165
- res . render ( "randomMeals" , {
166
- recipes : recipesWithNutrients ,
167
- } ) ;
168
- } catch ( error ) {
169
- res . send ( "Sorry! Some error occurred. Please try again!" ) ;
170
- console . log ( error ) ;
171
- }
172
- } ) ;
173
-
174
- app . get ( "/recipe/:mealId" , async ( req , res ) => {
175
- const mealId = req . params . mealId ;
176
-
177
- const { default : fetch } = await import ( "node-fetch" ) ;
178
-
179
- //Recipe info based on meal id
180
- const recipeInfo =
181
- await fetch ( `https://api.spoonacular.com/recipes/${ mealId } /information?apiKey=${ apiKey } &includeNutrition=true
182
- ` ) ;
183
- const info = await recipeInfo . json ( ) ;
184
-
185
- // Summary of the recipe based on meal id
186
-
187
- const summaryInfo = await fetch (
188
- `https://api.spoonacular.com/recipes/${ mealId } /summary?apiKey=${ apiKey } `
189
- ) ;
190
- const summary = await summaryInfo . json ( ) ;
191
-
192
- //Ingredient info based on meal id
193
- const ingredientsInfo =
194
- await fetch ( `https://api.spoonacular.com/recipes/${ mealId } /ingredientWidget.json?apiKey=${ apiKey }
195
- ` ) ;
196
- const ingredientsData = await ingredientsInfo . json ( ) ;
18
+ // Views middleware
19
+ app . set ( "view engine" , "ejs" ) ;
20
+ app . use ( express . static ( "public" ) ) ;
197
21
198
- res . render ( "recipe" , {
199
- title : info . title ,
200
- readyInMinutes : info . readyInMinutes ,
201
- image : info . image ,
202
- nutrition : info . nutrition ,
203
- summary : striptags ( summary . summary ) ,
204
- analyzedInstructions : info . analyzedInstructions ,
205
- ingredients : ingredientsData . ingredients ,
206
- } ) ;
207
- // res.send(summary.summary);
208
- } ) ;
22
+ // -----------------------------------
23
+ // ROUTERS
24
+ app . use ( "/" , viewsRoutes ) ;
25
+ app . post ( "/mealList" , weekMenusRoutes ) ;
26
+ app . post ( "/ingredientList" , fridgeRoutes ) ;
27
+ app . post ( "/randomList" , tagRoutes ) ;
28
+ app . get ( "/recipe/:mealId" , recipeRoutes ) ;
209
29
210
- module . exports . main = app ;
30
+ module . exports = app ;
0 commit comments