-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (93 loc) · 2.21 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require('dotenv').config()
const express = require('express')
const morgan = require('morgan')
const app = express()
const Person = require('./models/person')
app.use(express.json())
app.use(express.static('build'))
morgan.token('body', (req, res) => {
if (req.method === 'POST') {
return JSON.stringify(req.body)
} else {
return ''
}
})
app.use(
morgan(':method :url :status :res[content-length] - :response-time ms :body')
)
/** @type {(fn: (req: express.Request, res: express.Response) => Promise<any>)) => any} */
const asyncMw = (fn) => {
return (req, res, next) => fn(req, res, next).catch(next)
}
app.get(
'/api/persons/:id',
asyncMw(async (req, res) => {
const id = req.params.id
const thePerson = await Person.findById(id)
if (thePerson) {
res.json(thePerson)
} else {
res.status(404).end()
}
})
)
app.delete(
'/api/persons/:id',
asyncMw(async (req, res) => {
const id = req.params.id
await Person.findByIdAndDelete(id)
res.status(204).end()
})
)
app.get(
'/api/persons',
asyncMw(async (req, res) => {
const people = await Person.find({})
res.json(people)
})
)
app.post(
'/api/persons',
asyncMw(async (req, res) => {
if (!req.body.name) {
res.json({ error: 'name must be non-empty' })
return
}
if (!req.body.number) {
res.json({ error: 'number must be non-empty' })
return
}
const sameNamePerson = await Person.exists({ name: req.body.name })
if (sameNamePerson) {
res.json({ error: 'name must be unique' })
return
}
const newPerson = new Person({
name: req.body.name,
number: req.body.number,
})
await newPerson.save()
res.status(204).end()
})
)
app.get(
'/info',
asyncMw(async (req, res) => {
const entries = await Person.countDocuments()
res.send(`
<p>Phonebook has info for ${entries} people</p>
<p>${new Date()}</p>
`)
})
)
const errorHandler = (error, request, response, next) => {
if (error.name === 'CastError') {
return response.status(400).send({ error: 'malformatted id' })
}
next(error)
}
app.use(errorHandler)
const PORT = process.env.PORT || 3001
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`)
})