-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
36 lines (31 loc) · 1.38 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
const express = require('express')
const app = express()
const port = 3000
const exphbs = require('express-handlebars')
const restaurantList = require('./restaurant.json')
// setting template engine
app.engine('handlebars', exphbs({ defaultLayout: 'main' }))
app.set('view engine', 'handlebars')
// setting static files
app.use(express.static('public'))
app.get('/', (req, res) => {
res.render('index', { restaurants: restaurantList.results })
})
app.get('/search', (req, res) => {
const keyword = req.query.keyword
const restaurants = restaurantList.results.filter(restaurant => { return restaurant.name.toLocaleLowerCase().includes(keyword.toLocaleLowerCase()) })
if (restaurants.length === 0) {
res.render('error', { keyword: keyword })
} else { res.render('index', { restaurants: restaurants, keyword: keyword }) }
})
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('/category/:restaurant_category', (req, res) => {
const restaurantCategory = restaurantList.results.filter(restaurant => { return restaurant.category === req.params.restaurant_category })
res.render('index', { restaurants: restaurantCategory })
})
app.listen(port, () => {
console.log(`Express is listening on localhost:${port}`)
})