Skip to content

Assignment Completed #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 08-node-basics/asg-file-sorter/file-sorter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
*/
109 changes: 86 additions & 23 deletions 09-node-express-server/task-list-http-server/server.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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')
Expand All @@ -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
*/
*/




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;
}
});
}
1 change: 1 addition & 0 deletions 09-node-express-server/task-list-http-server/tasks.json
Original file line number Diff line number Diff line change
@@ -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"}