-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (82 loc) · 2.53 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
const express = require("express")
const app = express()
app.use(express.static("build"))
const bodyParser = require("body-parser")
const morgan = require("morgan")
app.use(bodyParser.json())
const cors = require("cors")
app.use(cors())
morgan.token("content", function (req, res) {
const body = req.body || {}
return JSON.stringify(body)
})
app.use(morgan(":method :url :content :status :res[content-length] - :response-time ms"
))
const Person = require("./models/person")
app.get("/", (req, res) => {
res.send("<h1>Hello World!</h1>")
})
app.get("/api/persons", (req, res) => {
Person
.find({})
.then(persons => {
res.json(persons.map(person=>Person.format(person)))
})
})
app.get("/api/persons/:id", (req, res) => {
Person
.findById(req.params.id)
.then(person => {
if(person) {
res.json(Person.format(person))
} else {
res.status(404).send({message:"person not found"})
}
})
})
app.get("/info", (req, res) => {
Person
.find({})
.then(persons => {
const sum = persons.length
const date = new Date()
res.send("<p>puhelinluettelossa " + sum + " ihmisen tiedot<p><p>"+ date +"</p>")
})
})
app.delete("/api/persons/:id", (req, res) => {
const id = req.params.id
Person.findByIdAndDelete(id)
.then(response => {
res.status(204).end()
})
})
app.post("/api/persons/", (req, res) => {
const person = new Person({
name: req.body.name,
number: req.body.number
})
Person.findOne({name:req.body.name})
.then(response => {
if(response) {
res.status(405).send({message:"Person is already in list. You should use PUT to update."})
} else {
person.save()
.then(person => {
console.log("lisätään henkilö " + person.name + " numero " + person.number + " luetteloon")
res.json(Person.format(person))
})
}
})
})
app.put("/api/persons/:id", (req, res) => {
const id = req.params.id
Person.findByIdAndUpdate(id)
.then(person => {
console.log("muutetaan henkilölle " + person.name + " numero " + person.number + " luetteloon")
res.json(Person.format(person))
})
})
const PORT = process.env.PORT || 3001
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`)
})