-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (58 loc) · 1.76 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
const express = require('express')
const app = express()
const Joi = require('joi')
const port = process.env.PORT || 3000
app.use(express.json())
//get all the notes
app.get('/api/notes', (req, res) => {
res.send(noteList)
})
const noteList = [
{id:1, noteBody: 'Bonjou'},
{id: 2, noteBody: 'Bonsoir'}
]
//Get a specific note
app.get('/api/notes/:id', (req,res) => {
const note = noteList.find( n => n.id === parseInt(req.params.id))
if(!note) return res.status(404).send("Note not found")
res.send(note.noteBody)
})
//Add a note
app.post('/api/notes', (req, res) => {
const { error } = validateNote(req.body)
if(error) return res.status(404).send(error.details[0].message)
const note = {
id: noteList.length + 1,
noteBody: req.body.noteBody
}
noteList.push(note)
res.send(note)
})
//Edit a note
app.put('/api/notes/:id', (req, res) => {
//check if note exist
const note = noteList.find( n => n.id === parseInt(req.params.id))
if(!note) return res.status(404).send("Note not found")
const { error } = validateNote(req.body)
if(error) return res.status(404).send(error.details[0].message)
note.noteBody = req.body.noteBody
res.send(note)
})
//Delete note
app.delete('/api/notes/:id', (req, res) => {
const note = noteList.find( n => n.id === parseInt(req.params.id))
if(!note) return res.status(404).send("Note not found")
const index = noteList.indexOf(note)
noteList.splice(index, 1)
res.send(note)
})
//Check if note has more than 2 character
function validateNote(note) {
const schema = {
noteBody: Joi.string().min(2).required()
}
return result = Joi.validate(note, schema)
}
app.listen(port, () => {
console.log(`server is up and running on ${port}`)
})