-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.js
60 lines (47 loc) · 1.93 KB
/
backend.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
// Tutorial: https://www.youtube.com/watch?v=FcwfjMebjTU&t=0s
import express from "express";
import cors from "cors";
import { config as dotenvConfig } from "dotenv";
import nodeFetch from "node-fetch";
const PORT = 8000;
// run: npm install express cors node-fetch dotenv
const app = express();
app.use(cors());
let fetch = nodeFetch;
dotenvConfig();
app.get("/", (req, res) => {
res.json("Server running on port 8000! Check port * 8000/backend * for data");
});
app.get("/backend", (req, res) => {
// console.log("req: ", req); // <-- request: IncomingMessage { ... }
// console.log("req.query: ", req.query); // <-- query: [Object: null prototype] {}
// options is required ~ Rapid API
const options = {
method: "GET",
headers: {
// "X-RapidAPI-Key": import.meta.env.VITE_RAPID_API_KEY, // -->> Default API key from Rapid API. imported for Vite.
"X-RapidAPI-Key": process.env.RAPID_API_KEY, // -->> To access env variables. In Node.js, env variables are accessed via process.env, not import.meta.env.
"X-RapidAPI-Host": "edamam-food-and-grocery-database.p.rapidapi.com",
},
};
// const url = `https://imdb8.p.rapidapi.com/auto-complete?q=+${endPoint}`; // IMDb API
const url =
"https://edamam-food-and-grocery-database.p.rapidapi.com/api/food-database/v2/parser?nutrition-type=cooking&category%5B0%5D=generic-foods&health%5B0%5D=alcohol-free";
// async function:
const fetchData = async () => {
try {
const response = await fetch(url, options);
const result = await response.json();
// console.log(result.hints);
// console.log(result.hints.food);
// setContainer(result.hints); // setContainer is now an array of objects that contains the data from the API
res.json(result.hints); // send data to frontend
} catch (error) {
console.error(error);
}
};
fetchData();
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});