aboutsummaryrefslogtreecommitdiff
path: root/practice/index.js
diff options
context:
space:
mode:
authorIbrahim Mkusa <iskm@users.noreply.github.com>2023-05-04 08:40:13 -0400
committerGitHub <noreply@github.com>2023-05-04 08:40:13 -0400
commit64dfcb8c4a4c9fc4dfaa6b7e4e8c2971461a4f04 (patch)
treed1dd877ba868e260956403ab84476bfb7891ca25 /practice/index.js
parent6610ee7aec692cd5c913a2853f16d3f1030c19bf (diff)
parent6b3877aac724d9692a099e23ca5ed1e1b17037be (diff)
Merge pull request #1 from iskm/fun-with-node
Fun with node
Diffstat (limited to 'practice/index.js')
-rw-r--r--practice/index.js53
1 files changed, 53 insertions, 0 deletions
diff --git a/practice/index.js b/practice/index.js
new file mode 100644
index 0000000..f6f51a5
--- /dev/null
+++ b/practice/index.js
@@ -0,0 +1,53 @@
+const express = require('express')
+const app = express()
+
+let notes = [
+ {
+ id: 1,
+ content: "HTML is easy",
+ important: true
+ },
+ {
+ id: 2,
+ content: "Browser can execute only JavaScript",
+ important: false
+ },
+ {
+ id: 3,
+ content: "GET and POST are the most important methods of HTTP protocol",
+ important: true
+ }
+]
+
+app.get('/', (request, response) => {
+ response.send('<h1>Habari Tanzania</h1>')
+})
+
+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)
+ notes = notes.filter(note => note.id !== id)
+
+ response.status(204).end()
+})
+
+const PORT = 3001
+app.listen(PORT)
+console.log(`Server running on port ${PORT}`)