diff --git a/08-node-basics/asg-file-sorter/file-sorter.js b/08-node-basics/asg-file-sorter/file-sorter.js index e682eb0..3d69509 100644 --- a/08-node-basics/asg-file-sorter/file-sorter.js +++ b/08-node-basics/asg-file-sorter/file-sorter.js @@ -3,4 +3,4 @@ * 1. Read files 1.txt, 2.txt, 3.txt and all the numbers inside them * 2. Sort the numbers in ascending order * 3. Write the sorted numbers to a file called sorted.txt - */ \ No newline at end of file + */ diff --git a/09-node-express-server/task-list-http-server/server.js b/09-node-express-server/task-list-http-server/server.js index c9c294a..f37eaf5 100644 --- a/09-node-express-server/task-list-http-server/server.js +++ b/09-node-express-server/task-list-http-server/server.js @@ -1,19 +1,27 @@ -const express = require('express') -const app = express() +const express = require("express"); +const fs=require('fs'); +const app = express(); -/** - * Imagine there is a list of tasks like this: - * 1. Enroll to Scaler +app.use(express.json({ extended: true })); +var taskList; +const fileName="tasks.json"; + + +read(); + +/** + * Imagine there is a list of tasks like this: + * 1. Enroll to Scaler * 2. Learn Node.js * 3. Learn React.js * 4. Get a job * 5. Get a girlfriend/boyfriend - * + * */ /** * When GET request is sent to 127.0.0.1:4114/tasks, - * response will be + * response will be * [ * 'Enroll to Scaler', * 'Learn Node.js', @@ -22,45 +30,78 @@ const app = express() * 'Get a girlfriend/boyfriend' * ] */ -app.get('/tasks', (req, res) => { -}) + +app.get("/tasks", (req, res) => { + res.send(taskList); + console.log(taskList); +}); /** * If GET request is sent to 127.0.0.1:4114/tasks/1, - * Then response will be - * + * Then response will be + * * 'Enroll to Scaler' - * + * * If GET request is sent to 127.0.0.1:4114/tasks/2 * Then response will be - * + * * 'Learn Node.js' */ -app.get('/tasks/:id', (req, res) => { +app.get("/tasks/:id", (req, res) => { + // BONUS: figure out how `:id` part works + const id=Number(req.params.id); + if(isNaN(id)){ + console.log(id); + return res.status(400).send('Invalid id'); + } - // BONUS: figure out how `:id` part works -}) + if(id<=0 || id>taskList.length){ + return res.status(404).send('Task not found'); + }else + res.send(taskList[id]); +}); +/** + * If POST request is sent to +}); +**/ app.delete('/tasks/:id', (req, res) => { // Delete task with given id + const id=Number(req.params.id); + if(isNaN(id)){ + console.log(id); + return res.status(400).send('Invalid id'); + } + else{ + delete taskList[id]; + write(taskList); + res.send("Deleted task with id :"+id +" successfully"); + } }) /** * When POST request is sent to 127.0.0.1:4114/tasks, * with the body: - * { "task": "Complete NodeJS Assignment" } - * + * { "task": "Complete NodeJS Assignment" } + * * Then a new task will be added to the list. - * + * * 6. Complete NodeJS Assignment */ -app.post('/tasks', (req, res) => { - -}) +app.post("/tasks", (req, res) => { + if (req.body.task == undefined) { + res.send("Please add a body to add a task. The format should be {\"task\": \"task name\"}"); + } else { + const index=Object.keys(taskList).length + 1; + taskList[index] = req.body.task; + write(taskList); + res.send("A new task " + req.body.task + " has been added to the list"); + } +}); app.listen(4114, () => { console.log('server started on http://127.0.0.1:4114') @@ -72,4 +113,26 @@ app.listen(4114, () => { * - Update the file every time a new task is created/deleted * - When server is restarted, old tasks should be available * - Read the file at server start to load the saved tasks - */ \ No newline at end of file + */ + + + + + function read(){ + fs.readFile(fileName, (err, data) => { + if (err) { + throw err; + } else { + taskList=JSON.parse(data); + console.log(taskList); + } + }); +} + +function write(data) { + fs.writeFile(fileName, JSON.stringify(data), (err) => { + if (err) { + throw err; + } + }); +} \ No newline at end of file diff --git a/09-node-express-server/task-list-http-server/tasks.json b/09-node-express-server/task-list-http-server/tasks.json new file mode 100644 index 0000000..4e07448 --- /dev/null +++ b/09-node-express-server/task-list-http-server/tasks.json @@ -0,0 +1 @@ +{"1":"Enroll to Scaler","2":"Learn Node.js","3":"Learn React.js","4":"Get a job","5":"Get a girlfriend","6":"Marry Her"} \ No newline at end of file