-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
147 lines (139 loc) · 3.77 KB
/
index.js
File metadata and controls
147 lines (139 loc) · 3.77 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import express from "express";
import axios from "axios";
import categories from "./options.json" assert { type: "json" };
const app = express();
const port = process.env.PORT || 3000;
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.get("/", async (req, res) => {
try {
const result = await axios.get(
"https://www.thecocktaildb.com/api/json/v1/1/random.php"
);
let drink = result.data.drinks[0];
let ingredients = [];
for (let i = 1; i < 10; i++) {
if (drink["strIngredient" + i] === null) {
break;
}
if (drink["strMeasure" + i] === null) {
drink["strMeasure" + i] = "";
}
const element =
drink["strMeasure" + i] + " " + drink["strIngredient" + i];
ingredients.push(element);
}
res.render("index.ejs", {
name: drink.strDrink,
id: drink.idDrink,
glass: drink.strGlass,
category: drink.strCategory,
instructions: drink.strInstructions,
thumb: drink.strDrinkThumb,
ingredients: ingredients,
alcoholic: drink.strAlcoholic,
category_a: categories.a,
category_c: categories.c,
category_g: categories.g,
category_i: categories.i,
});
} catch (error) {
console.log(error.response.message);
res.statusCode(404);
}
});
app.post("/search", async (req, res) => {
try {
req.body[Object.keys(req.body)] = decodeURIComponent(
req.body[Object.keys(req.body)]
);
let result = await axios.get(
"https://www.thecocktaildb.com/api/json/v1/1/filter.php",
{
params: req.body,
}
);
res.render("gallery.ejs", {
data: result.data["drinks"],
title: req.body[Object.keys(req.body)],
});
} catch (error) {
console.log(error);
}
});
app.post("/searchbar", async (req, res) => {
if (req.body.s !== "") {
try {
let result = await axios.get(
"https://www.thecocktaildb.com/api/json/v1/1/search.php",
{
params: req.body,
}
);
if (result.data["drinks"]) {
let title = req.body[Object.keys(req.body)].split("_").join(" ");
title = title[0].toUpperCase() + title.slice(1).toLowerCase();
res.render("gallery.ejs", {
data: result.data["drinks"],
title: title,
});
} else {
res.redirect("/");
}
} catch (error) {
console.log(error);
}
} else {
res.redirect("/");
}
});
app.get("/:id", async (req, res) => {
let id = req.url.slice(1);
try {
const result = await axios.get(
"http://www.thecocktaildb.com/api/json/v1/1/lookup.php",
{
params: {
i: id,
},
}
);
if (result.data.drinks) {
let drink = result.data.drinks[0];
let ingredients = [];
for (let i = 1; i < 10; i++) {
if (drink["strIngredient" + i] === null) {
break;
}
if (drink["strMeasure" + i] === null) {
drink["strMeasure" + i] = "";
}
const element =
drink["strMeasure" + i] + " " + drink["strIngredient" + i];
ingredients.push(element);
}
res.render("index.ejs", {
name: drink.strDrink,
id: drink.idDrink,
glass: drink.strGlass,
category: drink.strCategory,
instructions: drink.strInstructions,
thumb: drink.strDrinkThumb,
ingredients: ingredients,
alcoholic: drink.strAlcoholic,
category_a: categories.a,
category_c: categories.c,
category_g: categories.g,
category_i: categories.i,
});
} else{
res.redirect("/")
}
} catch (error) {
console.log(error.response.message);
res.statusCode(404);
}
});
app.listen(port, () => {
console.log(`Server started on port ${port}.`);
});