-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
38 lines (31 loc) · 1.06 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 app = express()
const port = 3000
const restaurantList = require('./restaurant.json')
const exphbs = require('express-handlebars')
app.engine('handlebars', exphbs({ defaultLayout: 'main' }))
app.set('view engine', 'handlebars')
app.use(express.static('public'))
app.get('/', (req, res) => {
res.render('index', { restaurants: restaurantList.results })
})
app.get('/restaurants/:restaurant_id', (req, res) => {
const restaurant = restaurantList.results.find(
(restaurant) => restaurant.id.toString() === req.params.restaurant_id
)
res.render('show', { restaurant: restaurant })
})
app.get('/search', (req, res) => {
const keyword= req.query.keyword
const restaurants=restaurantList.results.filter(restaurant=>{
return (
restaurant.name.toLowerCase().includes(keyword.toLowerCase()) ||
restaurant.category.toLowerCase().includes(keyword.toLowerCase())
)
}
)
res.render('index', { restaurants: restaurants })
})
app.listen(port, () => {
console.log(`Express is listening on localhost:${port}`)
})