-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
93 lines (81 loc) · 2.83 KB
/
routes.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
91
92
93
import express from 'express'
import axios from 'axios'
const router = express.Router()
const AUTH_QUERY = `?api_key=${process.env.REACT_APP_MDB_API_KEY}`
/* Retrieves most popular movies. Pagination enabled. */
router.get('/api', (req, res, next) => {
const paginationQuery = req.query.page
? `&page=${req.query.page}`
: ''
return axios.get(`https://api.themoviedb.org/3/movie/popular${AUTH_QUERY}${paginationQuery}`)
.then(response => {
res.status(200).send(response.data)
})
.catch(err => {
console.error(err)
res.redirect('/api/error')
})
})
/* Retrieves poster image for given movie based on its poster path/URL, found on the movie object. Can specify whether to retrieve the full version or the thumbnail version. */
router.get('/api/images/poster/:posterPath/:size', (req, res, next) => {
let { size, posterPath } = req.params
const width = size === 'full'
? `w${process.env.REACT_APP_POSTER_FULL_SIZE}`
: `w${process.env.REACT_APP_POSTER_THUMB_SIZE}`
const baseURL = process.env.REACT_APP_BASE_IMAGE_URL
// fetch poster in desired size from tmdb
const fullURL = `${baseURL}${width}/${posterPath}${AUTH_QUERY}`
return axios.get(fullURL, { responseType: 'arraybuffer' })
.then(response => {
// load image into buffer to send to react
const buffer = Buffer.from(response.data, 'binary').toString('base64')
res.status(200).send(buffer)
})
.catch(err => {
const { status, statusText, config } = err.response
console.error('ERROR!', status, statusText, config.url)
res.redirect('/api/error')
})
})
/* Retrieves a given movie based on its ID. */
router.get('/api/movie/:id', (req, res, next) => {
const movieId = req.params.id
return axios.get(`https://api.themoviedb.org/3/movie/${movieId}${AUTH_QUERY}`)
.then(response => {
res.status(200).send(response.data)
})
.catch(err => {
console.error(err)
res.redirect('/api/error')
})
})
/* Retrieves the cast for a given movie. */
router.get('/api/movie/:id/cast', (req, res, next) => {
const movieId = req.params.id
return axios.get(`https://api.themoviedb.org/3/movie/${movieId}/credits${AUTH_QUERY}`)
.then(response => {
const { cast } = response.data
res.status(200).send(cast.map(member => member.name))
})
.catch(err => {
console.error(err)
res.redirect('/api/error')
})
})
/* Searches movies. Pagination enabled. */
router.post('/api/search', (req, res, next) => {
const { page, query } = req.body
const paginationQuery = page ? `&page=${page}` : ''
return axios.post(`https://api.themoviedb.org/3/search/movie${AUTH_QUERY}&query=${query}${paginationQuery}`)
.then(response => {
res.status(200).send(response.data)
})
.catch(err => {
console.error(err)
res.redirect('/api/error')
})
})
router.get('/api/error', (req, res, next) => {
return res.status(500).send({ 'message': 'sorry! we messed up :('})
})
export default router