-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
53 lines (38 loc) · 1.38 KB
/
app.js
File metadata and controls
53 lines (38 loc) · 1.38 KB
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
// require packages used in the project
const express = require('express')
const app = express()
const port = 3000
// require express-handlebars here
const exphbs = require('express-handlebars')
// require json file here
const restaurantList = require('./restaurant.json')
// setting template engine
app.engine('handlebars', exphbs({ defaultLayout: 'main' }))
app.set('view engine', 'handlebars')
// setting static files: Javascripts & CSS
app.use(express.static('public'))
// routes setting
// index page displaying
app.get('/', (req, res) => {
// pass restaurants data into 'index' partial template
res.render('index', {restaurants: restaurantList.results})
})
// show page displaying
app.get('/restaurants/:restaurant_id', (req, res) => {
const restaurant = restaurantList.results.find(
restaurant => restaurant.id.toString() === req.params.restaurant_id //jason id type: number V.S id type past by route: string
)
res.render('show', { restaurant })
})
// search page displaying
app.get('/search', (req, res) => {
const keyword = req.query.keyword
const restaurants = restaurantList.results.filter(restaurant => {
return restaurant.name.toLowerCase().includes(keyword.toLowerCase())
})
res.render('index', { restaurants, keyword: keyword })
})
// start and listen on the Express server
app.listen(port, () => {
console.log(`Express is listening on localhost:${port}`)
})