-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (68 loc) · 2.18 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
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
const express = require("express");
const bodyParser = require("body-parser");
const fetch = require("node-fetch");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Starting app at http://localhost:${port}`);
});
app.use(bodyParser.json());
app.use(express.static("public"));
//index.js
app.get("/", (req, res) => {
res.sendFile("index.html", { root: path.join(__dirname, "public") });
});
//Api base url
const tmdbBaseUrl = "https://api.themoviedb.org/3";
//Get genres route
app.get("/api/getGenres", async (req, res) => {
const genreRequestEndpoint = "/genre/movie/list";
const urlToFetch = tmdbBaseUrl + genreRequestEndpoint;
const api_key = process.env.API_KEY;
const apiResponse = await fetch(urlToFetch, {
method: "GET",
headers: {
Authorization: "Bearer " + api_key,
"Content-Type": "application/json;charset=utf-8",
},
});
const jsonResponse = await apiResponse.json();
const data = jsonResponse.genres;
res.json(data);
});
//Get movies route
app.post("/api/getMovies", async (req, res) => {
const { genre, page } = req.body;
const discoverMovieEndpoint = "/discover/movie";
const requestParams = "?with_genres=" + genre + "&page=" + page;
const urlToFetch = tmdbBaseUrl + discoverMovieEndpoint + requestParams;
const api_key = process.env.API_KEY;
const apiResponse = await fetch(urlToFetch, {
method: "GET",
headers: {
Authorization: "Bearer " + api_key,
"Content-Type": "application/json;charset=utf-8",
},
});
const jsonResponse = await apiResponse.json();
const data = jsonResponse.results;
res.json(data);
});
//Get movieInfo route
//Get genres route
app.get("/api/getMovieInfo/:movieId", async (req, res) => {
const { movieId } = req.params;
const movieEndpoint = "/movie/" + movieId;
const urlToFetch = tmdbBaseUrl + movieEndpoint;
const api_key = process.env.API_KEY;
const apiResponse = await fetch(urlToFetch, {
method: "GET",
headers: {
Authorization: "Bearer " + api_key,
"Content-Type": "application/json;charset=utf-8",
},
});
const data = await apiResponse.json();
res.json(data);
});