-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
51 lines (43 loc) · 1.14 KB
/
server.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
40
41
42
43
44
45
46
47
48
49
50
51
const express = require('express')
const path = require('path')
const hbs = require('express-handlebars')
const { getPlaces, addPlace } = require('./utils')
const server = express()
// Server Config
server.use(express.static(path.join(__dirname, 'public')))
server.use(express.urlencoded({ extended: false }))
// Handlebars Middleware
server.engine('hbs', hbs({ extname: 'hbs' }))
server.set('view engine', 'hbs')
// Routes
server.get('/', (req, res) => {
getPlaces((err, places) => {
if (err) {
res.send(err.message)
}
const viewData = places
res.render('home', viewData)
})
})
server.get('/places/add', (req, res) => {
res.render('add')
})
server.post('/places/add', (req, res) => {
const newPlace = req.body
addPlace(newPlace, (err, newId) => {
if (err) {
res.send(err.message)
}
res.redirect(`/places/${newId}`)
})
})
server.get('/places/:id', (req, res) => {
getPlaces((err, placeData) => {
if (err) {
res.send(err.message)
}
const viewData = placeData.places.find(place => place.id === parseInt(req.params.id))
res.render('details', viewData)
})
})
module.exports = server