-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
35 lines (27 loc) · 1.14 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
const express = require('express');
const exphbs = require('express-handlebars');
const bodyParser = require('body-parser');
const app = express();
const restaurants = require('./restaurant.json');
app.engine('handlebars', exphbs({ defaultLayout: 'main' }));
app.set('view engine', 'handlebars');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
const port = 3000;
app.get('/', (req, res) => {
res.render('index', { restaurants: restaurants.results });
});
app.get('/restaurants/:id', (req, res) => {
const restaurant = restaurants.results.find(r => r.id.toString() === req.params.id);
res.render('show', { restaurant });
});
app.get('/search', (req, res) => {
const keyword = req.query.keyword.toLowerCase();
const filteredRestaurants = restaurants.results.filter(restaurant => {
return restaurant.name.toLowerCase().includes(keyword) || restaurant.category.toLowerCase().includes(keyword);
});
res.render('index', { restaurants: filteredRestaurants, keyword: req.query.keyword });
});
app.listen(port, () => {
console.log(`Express is listening on http://localhost:${port}`);
});