-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (78 loc) · 2.72 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
84
85
86
87
88
89
90
const axios = require("axios");
const express = require("express");
const cors = require("cors");
require("dotenv").config();
const PORT = 5000;
const trendingUrl = `https://api.themoviedb.org/3/trending/movie/week?api_key=${process.env.REACT_APP_API_KEY}`;
const topRatedUrl = `https://api.themoviedb.org/3/movie/top_rated?api_key=${process.env.REACT_APP_API_KEY}&language=en-US`;
const popularUrl = `https://api.themoviedb.org/3/movie/popular?api_key=${process.env.REACT_APP_API_KEY}&language=en-US`;
const searchUrl = `https://api.themoviedb.org/3/search/movie?&api_key=${process.env.REACT_APP_API_KEY}&query=`;
const app = express();
const router = express.Router();
app.use(cors());
app.use("/movies", router);
const getTrending = async (req, res) => {
try {
const pageNum = req.params.page;
const resp = await axios.get(`${trendingUrl}&page=${pageNum}`);
res.json(resp.data);
} catch (error) {
res.json({ message: error.message });
}
};
const getTopRated = async (req, res) => {
try {
const pageNum = req.params.page;
const resp = await axios.get(`${topRatedUrl}&page=${pageNum}`);
res.json(resp.data);
} catch (error) {
res.json({ message: error.message });
}
};
const getPopular = async (req, res) => {
try {
const pageNum = req.params.page;
const resp = await axios.get(`${popularUrl}&page=${pageNum}`);
res.json(resp.data);
} catch (error) {
res.json({ message: error.message });
}
};
const getSearch = async (req, res) => {
try {
const searchTerm = req.params.query;
const resp = await axios.get(`${searchUrl}${searchTerm}`);
res.json(resp.data);
} catch (error) {
res.json({ message: error.message });
}
};
const getMovieDetails = async (req, res) => {
try {
const id = req.params.id;
const resp = await axios.get(
`https://api.themoviedb.org/3/movie/${id}?api_key=${process.env.REACT_APP_API_KEY}&language=en-US&append_to_response=videos,similar,reviews,credits,watch/providers`
);
res.json(resp.data);
} catch (error) {
res.json({ message: error.message });
}
};
const getWatchProviders = async (req, res) => {
try {
const id = req.params.id;
const resp = await axios.get(
`https://api.themoviedb.org/3/movie/${id}/watch/providers?api_key=${process.env.REACT_APP_API_KEY}`
);
res.json(resp.data);
} catch (error) {
res.json({ message: error.message });
}
};
router.get("/trending/:page", getTrending);
router.get("/popular/:page", getPopular);
router.get("/topRated/:page", getTopRated);
router.get("/search/:query", getSearch);
router.get("/movieDetails/:id", getMovieDetails);
router.get("/watchProviders/:id", getWatchProviders);
app.listen(PORT, () => console.log(`Server is running on port ${PORT}`));