-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
38 lines (33 loc) · 1.11 KB
/
app.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
const express = require('express')
const { engine } = require('express-handlebars')
const app = express()
const port = 3000
const movies = require('./public/jsons/movies.json').results
const BASE_IMG_URL = 'https://movie-list.alphacamp.io/posters/'
app.engine('.hbs', engine({extname: '.hbs'}))
app.set('view engine', '.hbs')
app.set('views', './views')
app.use(express.static('public'))
app.get('/', (req, res) => {
res.redirect('/movies')
})
app.get('/movies', (req, res) => {
const keyword = req.query.search?.trim()
const matchedMovies = keyword ? movies.filter((mv) =>
Object.values(mv).some((property) => {
if (typeof property === 'string') {
return property.toLowerCase().includes(keyword.toLowerCase())
}
return false
})
) : movies
res.render('index', { movies: matchedMovies, BASE_IMG_URL, keyword })
})
app.get('/movie/:id', (req, res) => {
const id = req.params.id
const movie = movies.find((mv) => mv.id.toString() === id)
res.render('detail', { movie, BASE_IMG_URL })
})
app.listen(port, () => {
console.log(`express server is running on http://localhost:${port}`)
})