-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
35 lines (28 loc) · 1020 Bytes
/
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
const express = require('express')
const hdbars = require('express-handlebars')
const resturentsSrc = require('./restaurant.json')
const app = express()
const port = 3000
app.engine('handlebars', hdbars({defaultLayout:'main.handlebars'}))
app.set('view engine', 'handlebars')
app.use(express.static('public'))
app.listen(port, ()=>{
console.log('listening on port : ', port)
})
app.get('/',(req,res)=>{
const restaurants = resturentsSrc.results
console.log(restaurants)
res.render('index', {restaurants})
})
app.get('/search',(req,res)=>{
const {keyword} = req.query
const restaurants = resturentsSrc.results.filter((restaurant)=>{
return restaurant.name.toLowerCase().includes(keyword.toLowerCase())
})
res.render('index', {restaurants, keyword})
})
app.get('/restaurants/:restaurantId',(req, res)=>{
const {restaurantId} = req.params
const restaurant = resturentsSrc.results.find((ele)=> `${ele.id}` === `${restaurantId}`)
res.render('detail',{restaurant})
})