diff options
author | Ibrahim Mkusa <ibrahimmkusa@gmail.com> | 2023-05-04 08:07:32 -0400 |
---|---|---|
committer | Ibrahim Mkusa <ibrahimmkusa@gmail.com> | 2023-05-04 08:07:32 -0400 |
commit | ff4834445ba2a8d65c08ebbf6b422117494efd1a (patch) | |
tree | 6fa08fb927f6287b92aa5d081cc9dca354f814ad /practice/index.js | |
parent | 7bcd32a67804605c0d4322a9f9c8be4829dd5df7 (diff) |
implement REST with express
Diffstat (limited to 'practice/index.js')
-rw-r--r-- | practice/index.js | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/practice/index.js b/practice/index.js index f121c37..2abb422 100644 --- a/practice/index.js +++ b/practice/index.js @@ -27,6 +27,27 @@ app.get('/api/notes', (request, response) => { response.json(notes) }) +app.get('/api/notes/:id', (request, response) => { + const id = Number(request.params.id) + const note = notes.find(note => { + console.log(note.id, typeof note.id, id, typeof id, note.id === id) + return note.id === id + }) + console.log(note) + if (note) { + response.json(note) + } else { + response.status(404).end() + } +}) + +app.delete('/api/notes/:id', (request, response) => { + const id = Number(request.params.id) + const note = notes.filter(note => note.id !== id) + + response.status(204).end() +}) + const PORT = 3001 app.listen(PORT) console.log(`Server running on port ${PORT}`) |