Skip to content

Completed the task-list #23

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 3 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
59 changes: 58 additions & 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,61 @@
* 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
*/
*/



const fs = require("fs");
const { type } = require("os");

// const arr = [1, 2, 3, 4];
// var arr1 = "1,2,3,4,5";
// fs.writeFile("1.txt", arr1, (err) => {
// if (err) throw err;
// else {
// console.log("File Saved");
// }
// });

const finalArrayList = [];
const data1 = fs.readFileSync("1.txt", "utf8").split("\n"); // get data from files in string encoded in utf8
const data2 = fs.readFileSync("1.txt", "utf8").split("\n");
const data3 = fs.readFileSync("1.txt", "utf8").split("\n");

// parseInt from string to number and store in finalArrayList of all the three files content
for (i in data1) {
var num = parseInt(data1[i]);
finalArrayList.push(num);
}
for (i in data2) {
var num = parseInt(data1[i]);
finalArrayList.push(num);
}
for (i in data3) {
var num = parseInt(data1[i]);
finalArrayList.push(num);
}

// sort the integer Array
finalArrayList.sort((a, b) => a - b);

var contentToadd = "";

// store the integer array in string by adding the "\n" in each line

for (i in finalArrayList) {
// console.log(finalArrayList[i]);
contentToadd += finalArrayList[i];
contentToadd += "\n";
}
console.log(contentToadd);

// write the string contetToAdd in sorted-data file

fs.writeFile("sorted-data.txt", contentToadd, (err) => {
if (err) {
throw err;
} else {
console.log("File Saved!!");
}
});
83 changes: 78 additions & 5 deletions 09-node-express-server/task-list-http-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,83 @@ app.get('/tasks/:id', (req, res) => {
* 6. Complete NodeJS Assignment
*/

app.post('/tasks', (req, res) => {
const express = require("express");
const fs = require("fs");

const app = express();
app.use(express.urlencoded({ extended: true })); // to enable the parsing of the body if in it is of json format or in url-encoded fromat.
app.use(express.json());

const taskList = [
"Enroll to Scaler",
"Learn Node.Js",
"Learn React.Js",
"Get a Job",
"Get a Girlfriend",
];

saveTaskToFile = () => {
fs.writeFile("1.txt", taskList.toString().replace(",", "\n"), (err) => {
if (err) throw err;
else {
console.log("Saved the File\n");
}
});
};

const ln = taskList.length;
app.get("/tasks", (req, res) => {
res.send(taskList);
});
app.post("/tasks", (req, res) => {
const times = taskList.length - ln;
var strCur = req.body["task"];
strCur = strCur + " " + times;
taskList.push(strCur);

var curTaskToSave = "";
var str = taskList.toString();
for (let i = 0; i < str.length; i++) {
if (str[i] == ",") {
curTaskToSave += "\n";
} else {
curTaskToSave += str[i];
}
}
console.log(curTaskToSave);

fs.writeFile("task.json", curTaskToSave, (err) => {
if (err) throw err;
else {
console.log("Saved the File\n");
}
});

res.send(
"sending post request and aded the one more task! \n now task =" + times
);
});
saveTaskToFile;

app.get("/tasks/:id", (req, res) => {
// if path is like /:id then we can extract the string after / using req.params.id
if (req.params.id >= taskList.length || req.params.id < 0) {
res.send("This is not Authorised!!");
} else {
res.send(taskList[req.params.id]);
}
});

app.listen("3300", () => {
console.log("listening at port" + 3300);
});

// You can't send the body in get erequest.
/* Save the tasks to a file task.json
update the file every time a new taks is created
when server is restarted old task should be available
Read the file at server start to load the saved tasks
*/


})

app.listen(4114, () => {
console.log('server started on http://127.0.0.1:4114')
})