-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
86 lines (75 loc) · 1.35 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
const express = require("express")
const {
getAllMonkeys,
getMonkeyByName,
createMonkey
} = require("./model/monkey.model")
const app = express()
const port = 3000
app.use(express.json())
app.get("/", (req, res) => {
res.status(200).send({
msg: "this is working"
})
})
app.get("/monkey", async (req, res) => {
console.log("get monkey")
const name = req.query.name
if(!name) {
res.status(400).send({
msg: "Need a name"
})
return
}
try {
if (name === "all") {
const monkey = await getAllMonkeys()
if (monkey.length === 0) {
res.status(404).send({
msg: "No monkey found"
})
return
}
res.status(200).send({
monkey: monkey
})
return
}
const monkey = await getMonkeyByName(name)
if (!monkey) {
res.status(404).send({
msg: "No monkey found"
})
return
}
res.status(200).send({
monkey: monkey
})
} catch (error) {
res.status(500).send({
msg: "Internal Server Error"
})
}
})
app.post("/monkey", async (req, res) => {
const name = req.body.name
if (!name) {
res.status(400).send({
msg: "Need a name"
})
return
}
try {
await createMonkey(name)
res.status(201).send({
msg: "Monkey created",
})
} catch (error) {
res.status(500).send({
msg: "Internal Server Error"
})
}
})
app.listen(port, () => {
console.log(`Server is listening on port ${port}`)
})