-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (40 loc) · 1.2 KB
/
index.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
const express = require('express');
const axios = require('axios')
const app = express();
const port = 3000;
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/recipe', async (req, res) => {
try {
const response = await axios.get('https://www.themealdb.com/api/json/v1/1/random.php');
const recipe = response.data.meals[0];
const name = recipe.strMeal;
const instructions = recipe.strInstructions;
const tutorial = recipe.strSource;
const link = recipe.strMealThumb;
const ingredients = [];
for (let i = 1; i <= 20; i++) {
const ingredient = recipe['strIngredient' + i];
const measure = recipe['strMeasure' + i];
if (ingredient && measure) {
ingredients.push(`${ingredient} (${measure})`);
}
}
const recipeObj = {
name,
instructions,
ingredients,
link,
tutorial
};
res.json(recipeObj);
} catch (error) {
console.error(error);
res.status(500).send('Error fetching recipe');
}
});
app.listen(port, () => {
console.log(`Server started at http://localhost:${port}`);
});