From b093f8939fa6e3b7e6c843076b3516f242c3f3cb Mon Sep 17 00:00:00 2001 From: Zaid Karigar Date: Wed, 22 Nov 2023 20:56:51 +0530 Subject: [PATCH] Completed the hard-Todo App server --- 02-nodejs/package-lock.json | 111 ++++++++++++++++++++++++++++++------ 02-nodejs/package.json | 2 + 02-nodejs/todoServer.js | 92 ++++++++++++++++++++++++++++++ 02-nodejs/todos.json | 32 +++++++++++ 4 files changed, 221 insertions(+), 16 deletions(-) create mode 100644 02-nodejs/todos.json diff --git a/02-nodejs/package-lock.json b/02-nodejs/package-lock.json index b13088b7..ac4b2c98 100644 --- a/02-nodejs/package-lock.json +++ b/02-nodejs/package-lock.json @@ -9,7 +9,9 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "body-parser": "^1.20.2", "express": "^4.18.2", + "fs": "^0.0.1-security", "uuid": "^9.0.0" }, "devDependencies": { @@ -1265,12 +1267,12 @@ "dev": true }, "node_modules/body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "version": "1.20.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", + "integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==", "dependencies": { "bytes": "3.1.2", - "content-type": "~1.0.4", + "content-type": "~1.0.5", "debug": "2.6.9", "depd": "2.0.0", "destroy": "1.2.0", @@ -1278,7 +1280,7 @@ "iconv-lite": "0.4.24", "on-finished": "2.4.1", "qs": "6.11.0", - "raw-body": "2.5.1", + "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" }, @@ -1882,6 +1884,29 @@ "node": ">= 0.10.0" } }, + "node_modules/express/node_modules/body-parser": { + "version": "1.20.1", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", + "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "dependencies": { + "bytes": "3.1.2", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "on-finished": "2.4.1", + "qs": "6.11.0", + "raw-body": "2.5.1", + "type-is": "~1.6.18", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8", + "npm": "1.2.8000 || >= 1.4.16" + } + }, "node_modules/express/node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -1909,6 +1934,20 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/express/node_modules/raw-body": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", + "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "dependencies": { + "bytes": "3.1.2", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/fast-json-stable-stringify": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", @@ -2030,6 +2069,11 @@ "node": ">= 0.6" } }, + "node_modules/fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==" + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -3557,9 +3601,9 @@ } }, "node_modules/raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz", + "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==", "dependencies": { "bytes": "3.1.2", "http-errors": "2.0.0", @@ -5270,12 +5314,12 @@ "dev": true }, "body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "version": "1.20.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", + "integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==", "requires": { "bytes": "3.1.2", - "content-type": "~1.0.4", + "content-type": "~1.0.5", "debug": "2.6.9", "depd": "2.0.0", "destroy": "1.2.0", @@ -5283,7 +5327,7 @@ "iconv-lite": "0.4.24", "on-finished": "2.4.1", "qs": "6.11.0", - "raw-body": "2.5.1", + "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" }, @@ -5720,6 +5764,25 @@ "vary": "~1.1.2" }, "dependencies": { + "body-parser": { + "version": "1.20.1", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", + "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "requires": { + "bytes": "3.1.2", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "on-finished": "2.4.1", + "qs": "6.11.0", + "raw-body": "2.5.1", + "type-is": "~1.6.18", + "unpipe": "1.0.0" + } + }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -5740,6 +5803,17 @@ "requires": { "side-channel": "^1.0.4" } + }, + "raw-body": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", + "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "requires": { + "bytes": "3.1.2", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + } } } }, @@ -5845,6 +5919,11 @@ "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", "integrity": "sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q==" }, + "fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==" + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -6968,9 +7047,9 @@ "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==" }, "raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz", + "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==", "requires": { "bytes": "3.1.2", "http-errors": "2.0.0", diff --git a/02-nodejs/package.json b/02-nodejs/package.json index 4858530a..bb8918f1 100644 --- a/02-nodejs/package.json +++ b/02-nodejs/package.json @@ -21,7 +21,9 @@ "testTimeout": 10000 }, "dependencies": { + "body-parser": "^1.20.2", "express": "^4.18.2", + "fs": "^0.0.1-security", "uuid": "^9.0.0" } } diff --git a/02-nodejs/todoServer.js b/02-nodejs/todoServer.js index cffc7d60..09fbbd2e 100644 --- a/02-nodejs/todoServer.js +++ b/02-nodejs/todoServer.js @@ -41,9 +41,101 @@ */ const express = require('express'); const bodyParser = require('body-parser'); +const fs= require('fs'); const app = express(); +const port= 3000; app.use(bodyParser.json()); +function findIdIdx(arr, id){ + for(let i=0; i { + //Retrieve all todo items + fs.readFile("./todos.json", "utf-8", (err, data) => { + if(err) res.send(err) + res.status(200).send(data) + }) +}) + +app.get("/todos/:id", (req, res) => { + //Retrieve a specific todo item by ID + + fs.readFile("./todos.json", "utf-8", (err, data) => { + if(err) res.status(500).send("Error loading items") + + const todoItems= JSON.parse(data) + const result= findIdIdx(todoItems, parseInt(req.params.id)) + + if(result === -1) + res.status(404).send(`ID not found!`) + else + res.status(200).json(todoItems[result]) + }) +}) + +app.post("/todos", (req, res) => { + //Create a new todo item + const newTodo= req.body + fs.readFile("./todos.json", "utf-8", (err, data) => { + if(err) res.status(500).send("file note open") + var todoItems= JSON.parse(data) + todoItems.push(newTodo) + + fs.writeFile("./todos.json", JSON.stringify(todoItems), "utf-8", (wrErr) => { + if(wrErr) res.status(500).send("Error: Item not Added") + res.status(201).send(`Success: Item added ${JSON.stringify(newTodo)}`) + }) + }) +}) + +app.put("/todos/:id", (req, res) => { + //Update an existing todo item by ID + const id= parseInt(req.params.id) + const newTodo= req.body + fs.readFile("./todos.json", "utf-8", (err, data) => { + if(err) res.status(500).send("file not open") + var todoItems= JSON.parse(data) + const idx= findIdIdx(todoItems, id) + if(idx === -1) + res.status(404).send("Item ") + todoItems.splice(idx, 1) + todoItems.push(newTodo) + + fs.writeFile("./todos.json", JSON.stringify(todoItems), (wrErr) => { + if(wrErr) res.status(500).send("error: file not updated!") + res.status(200).send("Success: Item was found and updated") + }) + }) +}) + +app.delete("/todos/:id", (req, res) => { + //Delete a todo item by ID + const id= parseInt(req.params.id) + fs.readFile("./todos.json", "utf-8", (err, data) => { + if(err) res.status(500).send("file not open") + var todoItems= JSON.parse(data) + const idx= findIdIdx(todoItems, id) + if(idx === -1) + res.status(404).send("Item ") + todoItems.splice(idx, 1) + + fs.writeFile("./todos.json", JSON.stringify(todoItems), (wrErr) => { + if(wrErr) res.status(500).send("error: file not updated!") + res.status(200).send("Success: Item was Deleted") + }) + }) +}) + +app.listen(port, () => { + console.log(`listning on port ${port}`) +}) + module.exports = app; diff --git a/02-nodejs/todos.json b/02-nodejs/todos.json new file mode 100644 index 00000000..7eb7d497 --- /dev/null +++ b/02-nodejs/todos.json @@ -0,0 +1,32 @@ +[ + { + "id": 1, + "title": "homework", + "completed": false, + "desc": "complete your homework before evening." + }, + { + "id": 2, + "title": "clean the house", + "completed": false, + "desc": "clean the all rooms except first floor bedroom." + }, + { + "id": 3, + "todo": "do laundry", + "completed": false, + "desc": "wash clothes and dry them" + }, + { + "id": 5, + "todo": "learn Backend Development", + "completed": true, + "desc": "complete the harkirat's course and practice new things" + }, + { + "id": 6, + "todo": "Go to gym", + "completed": false, + "desc": "complete the leg day challenge" + } +]