-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
41 lines (35 loc) · 1.22 KB
/
app.js
File metadata and controls
41 lines (35 loc) · 1.22 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
const express = require('express')
const app = express()
const exphbs = require('express-handlebars')
const restaurantList = require('./restaurant.json')
const port = 3000
//use static public
app.use(express.static('public'))
//setting template engine
app.engine('handlebars',exphbs({ defaultLayout: 'main'}))
app.set('view engine','handlebars')
//route index setting
app.get('/', (req, res) => {
res.render('index',{restaurant: restaurantList.results})
})
// show setting
app.get('/restaurants/:restaurant_id', (req, res) => {
const showRestaurant = restaurantList.results.filter(restaurant => restaurant.id == req.params.restaurant_id)
res.render('show', {restaurant: showRestaurant[0]})
})
//search setting
app.get('/search',(req, res) => {
const keyword = req.query.keyword
const searchResult = restaurantList.results.filter(restaurant => {
return restaurant.name.toLowerCase().includes(keyword) || restaurant.category.toLowerCase().includes(keyword)
})
if (searchResult.length === 0){
res.render('noresults',{keyword})
}else {
res.render('index',{restaurant: searchResult, keyword: keyword})
}
})
//route listen
app.listen(port, () => {
console.log(`Express is running on http://localhost:${port}`)
})