-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
115 lines (93 loc) · 3.18 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
const express = require('express')
const bodyParser = require('body-parser')
const ejs = require('ejs')
const dotenv = require('dotenv')
const _ = require('lodash')
const rp = require('request-promise')
dotenv.config()
const app = express()
app.use(bodyParser.urlencoded({ extended: true }))
const apiKEY = process.env.API_KEY
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.get('/', function (req, res) {
const getRandomFoods = [
`https://api.spoonacular.com/recipes/random?apiKey=${apiKEY}&number=12`,
`https://api.spoonacular.com/food/jokes/random?apiKey=${apiKEY}`,
]
const promises = getRandomFoods.map((getRandomFood) => rp(getRandomFood))
Promise.all(promises).then((data) => {
const getRandomFoodsData = JSON.parse(data[0])
const getRandomJokesData = JSON.parse(data[1])
res.render('index', {
getRandomRecipes: getRandomFoodsData,
getRandomJokes: getRandomJokesData,
kebabCase: _.kebabCase,
})
}).catch(function(err) {
res.render("error", {
errMsg: err,
})
});
})
app.post('/', function (req, res) {
const searchQuery = _.kebabCase(req.body.searchQuery)
res.redirect(`/search/${searchQuery}`)
})
app.get('/search/:searchQuery', function (req, res) {
const query = req.params.searchQuery
const searchFoods = [
`https://api.spoonacular.com/food/search?query=${query}&apiKey=${apiKEY}&number=18`,
]
// console.log(searchFoods[0]);
const promises = searchFoods.map((searchFood) => rp(searchFood))
Promise.all(promises).then((data) => {
const searchRecipesByQuery = JSON.parse(data[0])
res.render('search', {
searchRecipesByQuery: searchRecipesByQuery,
kebabCase: _.kebabCase,
capitalize: _.capitalize,
lowerCase: _.lowerCase,
})
}).catch(function(err) {
res.render("error", {
errMsg: err,
})
});
})
app.get('/recipes/:recipeName=:foodId', function (req, res) {
const id = req.params.foodId
const requestFoodByIds = [
`https://api.spoonacular.com/recipes/${id}/information?apiKey=${apiKEY}`,
`https://api.spoonacular.com/recipes/${id}/similar?apiKey=${apiKEY}&number=6`,
`https://api.spoonacular.com/recipes/${id}/equipmentWidget.json?apiKey=${apiKEY}`,
`https://api.spoonacular.com/recipes/${id}/analyzedInstructions?apiKey=${apiKEY}`,
]
const promises = requestFoodByIds.map((requestFoodById) =>
rp(requestFoodById)
)
Promise.all(promises).then((data) => {
const getRecipeInformation = JSON.parse(data[0])
const getSimilarRecipes = JSON.parse(data[1])
const getRecipeEquipmentById = JSON.parse(data[2])
const getAnalyzedRecipeInstructions = JSON.parse(data[3])
res.render('recipe', {
getRecipeInformation: getRecipeInformation,
getSimilarRecipes: getSimilarRecipes,
getRecipeEquipmentById: getRecipeEquipmentById,
getAnalyzedRecipeInstructions: getAnalyzedRecipeInstructions,
kebabCase: _.kebabCase,
capitalize: _.capitalize,
})
}).catch(function(err) {
res.render("error", {
errMsg: err,
})
});
})
app.use(function (req, res) {
res.redirect('/')
})
app.listen(process.env.PORT, function () {
console.log('server is running on port ', process.env.PORT)
})