|
| 1 | +// const dotenv = require("dotenv"); |
| 2 | +// dotenv.config({ path: "./config.env" }); |
| 3 | +const express = require("express"); |
| 4 | +const bodyParser = require("body-parser"); |
| 5 | +const striptags = require("striptags"); |
| 6 | +const cheerio = require("cheerio"); |
| 7 | + |
| 8 | +const ejs = require("ejs"); |
| 9 | + |
| 10 | +const app = express(); |
| 11 | + |
| 12 | +app.set("view engine", "ejs"); |
| 13 | +app.use(bodyParser.urlencoded({ extended: true })); |
| 14 | +app.use(express.static("public")); |
| 15 | + |
| 16 | +const apiKey = "c7d40af634094a53a02a542268b9f073"; |
| 17 | + |
| 18 | +// All the get request to render differrent 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 | +// POST request to render menus for a week based on user input |
| 33 | + |
| 34 | +app.post("/mealList", async (req, res) => { |
| 35 | + // Variables for meal planning based on user input |
| 36 | + const targetCalories = req.body.targetCalories; |
| 37 | + let diets = req.body.diets; |
| 38 | + if (typeof diets === "string") { |
| 39 | + diets = req.body.diets.split(","); |
| 40 | + } |
| 41 | + let cuisines = req.body.cusines; |
| 42 | + if (typeof cuisines === "string") { |
| 43 | + cuisines = req.body.cuisines.split(","); |
| 44 | + } |
| 45 | + let excludeIngredients = req.body.excludeIngredients; |
| 46 | + if (typeof excludeIngredients === "string") { |
| 47 | + excludeIngredients = req.body.excludeIngredients.split(","); |
| 48 | + } |
| 49 | + let includeIngredients = req.body.includeIngredients; |
| 50 | + if (typeof includeIngredients === "string") { |
| 51 | + includeIngredients = req.body.includeIngredients.split(","); |
| 52 | + } |
| 53 | + let preferences = req.body.preferences; |
| 54 | + if (typeof preferences === "string") { |
| 55 | + preferences = req.body.preferences.split(","); |
| 56 | + } |
| 57 | + |
| 58 | + try { |
| 59 | + const { default: fetch } = await import("node-fetch"); |
| 60 | + |
| 61 | + // Week meal plan using user input |
| 62 | + const weekPlan = await fetch( |
| 63 | + `https://api.spoonacular.com/mealplanner/generate?apiKey=${apiKey}&timeFrame=week&targetCalories=${targetCalories}&diet=${diets}&excludeIngredients=${excludeIngredients}&includeIngredients=${includeIngredients}&cuisine=${cuisines}&preferences=${preferences}` |
| 64 | + ); |
| 65 | + |
| 66 | + const dataOfMeal = await weekPlan.json(); |
| 67 | + const days = await dataOfMeal.week; |
| 68 | + |
| 69 | + res.render("planMeals", { meals: days }); |
| 70 | + } catch (error) { |
| 71 | + res.send("Sorry! Some error occurred. Please try again!"); |
| 72 | + console.log(error); |
| 73 | + } |
| 74 | +}); |
| 75 | + |
| 76 | +//POST request to generate recipe based on ingredients provided by user |
| 77 | + |
| 78 | +app.post("/ingredientList", async (req, res) => { |
| 79 | + // Storing the input provided by user in a variable in backend |
| 80 | + |
| 81 | + let ingredients = req.body.ingredients; |
| 82 | + if (typeof ingredients === "string") { |
| 83 | + ingredients = req.body.ingredients.split(","); |
| 84 | + } |
| 85 | + |
| 86 | + let number = req.body.number; |
| 87 | + if (typeof number === "string") { |
| 88 | + number = req.body.number.split(","); |
| 89 | + } |
| 90 | + |
| 91 | + try { |
| 92 | + const { default: fetch } = await import("node-fetch"); |
| 93 | + |
| 94 | + //Generate recipes by ingredients |
| 95 | + const generateRecipesByIngredients = await fetch( |
| 96 | + `https://api.spoonacular.com/recipes/findByIngredients?ingredients=${ingredients}&number=${number}&apiKey=${apiKey}` |
| 97 | + ); |
| 98 | + const recipesData = await generateRecipesByIngredients.json(); |
| 99 | + |
| 100 | + // Generate recipe information |
| 101 | + |
| 102 | + const recipeInfo = await fetch( |
| 103 | + `https://api.spoonacular.com/recipes/complexSearch?apiKey=c7d40af634094a53a02a542268b9f073` |
| 104 | + ); |
| 105 | + const results = recipeInfo.json(); |
| 106 | + |
| 107 | + res.render("planMealsByIngredients", { |
| 108 | + recipes: recipesData, |
| 109 | + results: results, |
| 110 | + }); |
| 111 | + |
| 112 | + // res.send(recipesData); |
| 113 | + } catch (error) { |
| 114 | + res.send(error); |
| 115 | + console.log(error); |
| 116 | + } |
| 117 | +}); |
| 118 | + |
| 119 | +app.get("/recipe/:mealId", async (req, res) => { |
| 120 | + const mealId = req.params.mealId; |
| 121 | + |
| 122 | + const { default: fetch } = await import("node-fetch"); |
| 123 | + |
| 124 | + //Recipe info based on meal id |
| 125 | + const recipeInfo = |
| 126 | + await fetch(`https://api.spoonacular.com/recipes/${mealId}/information?apiKey=${apiKey}&includeNutrition=true |
| 127 | + `); |
| 128 | + const info = await recipeInfo.json(); |
| 129 | + |
| 130 | + // Summary of the recipe based on meal id |
| 131 | + |
| 132 | + const summaryInfo = await fetch( |
| 133 | + `https://api.spoonacular.com/recipes/${mealId}/summary?apiKey=${apiKey}` |
| 134 | + ); |
| 135 | + const summary = await summaryInfo.json(); |
| 136 | + |
| 137 | + //Ingredient info based on meal id |
| 138 | + const ingredientsInfo = |
| 139 | + await fetch(`https://api.spoonacular.com/recipes/${mealId}/ingredientWidget.json?apiKey=${apiKey} |
| 140 | + `); |
| 141 | + const ingredientsData = await ingredientsInfo.json(); |
| 142 | + |
| 143 | + res.render("recipe", { |
| 144 | + title: info.title, |
| 145 | + readyInMinutes: info.readyInMinutes, |
| 146 | + image: info.image, |
| 147 | + nutrition: info.nutrition, |
| 148 | + summary: striptags(summary.summary), |
| 149 | + analyzedInstructions: info.analyzedInstructions, |
| 150 | + ingredients: ingredientsData.ingredients, |
| 151 | + }); |
| 152 | + // res.send(summary.summary); |
| 153 | +}); |
| 154 | + |
| 155 | +module.exports.main = app; |
0 commit comments