-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
39 lines (32 loc) · 1.29 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
39
// require package used in the project
const express = require('express')
const app = express()
const port = 3000
// require express-handlebars here
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'))
// routes setting
app.get('/', (req, res) => {
// past the restaurant data into 'index' partial template
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, keyword: keyword })
})
// start and listen on the Express server
app.listen(port, () => {
console.log(`Express is listening on localhost:${port}`)
})