-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (63 loc) · 1.75 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require('dotenv').config()
const express = require('express')
const app = express()
app.use(express.json())
const Marker = require('./models/marker')
app.use(express.json())
const cors = require('cors')
app.use(express.static('dist'))
app.use(cors())
app.get('/api/markers', (_request, response) => {
Marker.find({}).then(markers => {
response.json(markers)
})
})
app.get('/api/markers/:id', (request, response, next) => {
Marker.findById(request.params.id)
.then(marker => {
if(marker) {
response.json(marker)
}else {
response.status(404).end()
}
})
.catch(error => next(error))
})
app.post('/api/markers', (request, response, next) => {
const body = request.body
const marker = new Marker({
name: body.name,
description: body.description,
latitude: body.latitude,
longitude: body.longitude,
})
marker.save().then(marker => {
response.json(marker)
})
.catch(error => next(error))
})
app.put('/api/markers/:id', (request, response, next) => {
const body = request.body
const marker = {
name: body.name,
description: body.description,
latitude: body.latitude,
longitude: body.longitude
}
Marker.findByIdAndUpdate(request.params.id, marker, { new: true })
.then(updatedMarker=> {
response.json(updatedMarker)
})
.catch(error => next(error))
})
app.delete('/api/markers/:id', (request, response, next) => {
Marker.findByIdAndRemove(request.params.id)
.then(() => {
response.status(204).end()
})
.catch(error => next(error))
})
const PORT = process.env.PORT
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`)
})