Skip to content

File-sorter_Task_Server_Assignment #9

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
55 changes: 54 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,57 @@
* 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');


all_data_from_files = [];


const number_of_files = 3; //Number of text files.
function compareNumbers(a, b) {
return a - b;
}
function to_add_values(array, flag) { //Function to add values to all_data_from_files array.
for (var i = 0; i < array.length; ++i) {
all_data_from_files.push(array[i]);
}
if (flag === true) {
to_sort_and_add();
}
}
function to_read(number) { //Function to read data from files
for (let i = 1; i <= number_of_files; ++i) {
fs.readFile(`${i}.txt`, (err, data) => {
if (err) {
throw err;
}
else {
console.log(`Successfully read ${i}.txt and added values from it to all_data_from_files array!`);
if (i === number_of_files) {
to_add_values(data.toString().split('\n'), false);
}
else {
to_add_values(data.toString().split('\n'), true);
}

}
})
}
}
function to_sort_and_add() { //Function to sort the final array and add values to sorted.txt file
all_data_from_files.sort(compareNumbers)
fs.writeFile(`sorted.txt`, all_data_from_files.toString(), (err) => {
if (err) {
throw err;
}
else {
console.log(`File Saved!`);
}
});

}

to_read(number_of_files);

34 changes: 30 additions & 4 deletions 09-node-express-server/task-list-http-server/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const express = require('express')
const app = express()

app.use(express.urlencoded({ extended: true }));
app.use(express.json());

/**
* Imagine there is a list of tasks like this:
* 1. Enroll to Scaler
Expand All @@ -22,8 +25,18 @@ const app = express()
* 'Get a girlfriend/boyfriend'
* ]
*/
app.get('/tasks', (req, res) => {

var taskList = [ //To store all the tasks.
"Enroll to Scaler",
"Learn Node.js",
"Learn React.js",
"Get a job",
"Get a girlfriend/boyfriend",
];


app.get('/tasks', (req, res) => {
res.send(taskList);
})

/**
Expand All @@ -39,8 +52,15 @@ app.get('/tasks', (req, res) => {
*/

app.get('/tasks/:id', (req, res) => {

let id_of_this_search = req.params.id;
if (taskList.length < id_of_this_search) { //Check if id is valid or not
res.send('Please try again with a valid entry!');
}
else {
res.send(taskList[id_of_this_search - 1]);
}
// BONUS: figure out how `:id` part works

})

/**
Expand All @@ -54,9 +74,15 @@ app.get('/tasks/:id', (req, res) => {
*/

app.post('/tasks', (req, res) => {

if (req.query["task"] === undefined) { //Check if task(query) is valid or not
res.send(`Cannot add to task list,please provide a task to add!!`);
}
else {
taskList.push(req.query["task"]);
res.send(`Task ${req.query["task"]} is successfully added to tasks list`);
}
})

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