Skip to content
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

Done with fileServer.js file #334

Open
wants to merge 2 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
41 changes: 41 additions & 0 deletions 02-nodejs/fileServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,47 @@ const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 3000;
var fileNames;
const folderPath = '/Users/namanagrawal/Desktop/Codes/100x/Assignments/Week_2/Week-2-Assignments/02-nodejs/files';

app.listen(port,()=>{
console.log('Listening in port - '+ port);
})

function MiddlewareToReadFilesName(req,res,next) {
fs.readdir(folderPath,(err,files)=>{
if(err){
console.log("Error in reading the Files name - "+err);
return;
}
else{
fileNames = files;
next();
}
})
}
app.use(MiddlewareToReadFilesName);

function getFilesName(req,res){
res.status(200).send(fileNames);
}

function giveFileData(req,res){
var filePath = folderPath+"/"+req.params.filename;
console.log(filePath);
fs.readFile(filePath,'utf8',(err,data)=>{
if(err){
res.status(404).send("Incorrect File selected");
console.log("Error in getting the file data - " + err);
}
else{
res.status(200).send(data);
}
})
}

app.get("/files",getFilesName);
app.get("/file/:filename",giveFileData);

module.exports = app;
113 changes: 111 additions & 2 deletions 02-nodejs/todoServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,120 @@

Testing the server - run `npm run test-todoServer` command in terminal
*/
const fs = require('fs')
const express = require('express');
const bodyParser = require('body-parser');
const { log } = require('console');
app = express()
var port = 3000;
app.listen(port,()=>{
console.log(`Listening on port - ${port}`);
})

const app = express();


var filePath = '/Users/namanagrawal/Desktop/Codes/100x/Assignments/Week_2/Week-2-Assignments/02-nodejs/ToDoList.txt'

let toDos = [];
app.use(bodyParser.json());

module.exports = app;
//Middleware to get the data from the file.

app.use((req,res,next)=>{
fs.readFile(filePath,'utf8',(err,data)=>{
if(err){
console.log("Error in reading File");
}
toDos = JSON.parse(data);
})
next();
})

function IdFinder(id) {
for(var i=0;i<toDos.length;i++){
if(toDos[i].id === id){
return i;
}
}
return -1;
}

function requestPost(req,res){
res.status(200).send(toDos);
}
function requestIdElement(req,res){
console.log(req.params.id);
const index = IdFinder(parseInt(req.params.id));
console.log("Index at - "+index);
if(index==-1){
res.status(404).send(req.params.id + " - ID not found!")
}
else{
res.status(200).send(toDos[index])
}
}
function addingElement(req,res) {
toDos.push(req.body)
fs.writeFile(filePath,JSON.stringify(toDos),(err,data)=>{
if(err){
res.status(404);
console.log("Error in writing the file back!!")
}
})
res.status(201);
console.log(toDos);
}
function modifyingElement(req,res){
var index = IdFinder(parseInt(req.params.id));

if(index==-1){
res.status(404);
console.log("ID Doesn't Exits!");
}
else{
toDos[index].title = req.body.title;
toDos[index].completed = req.body.completed;
toDos[index].description = req.body.description;
console.log(toDos);
fs.writeFile(filePath,JSON.stringify(toDos),(err,data)=>{
if(err){
res.status(404);
console.log("Error in writing the file back!!")
}
})

}
}


function deleteElement(req,res){
var index = IdFinder(parseInt(req.params.id));
console.log(index);
if(index==-1){
res.status(404);
console.log("ID Doesn't Exits!");
}
else{
res.status(200);
toDos.splice(index,1);
console.log(toDos);
fs.writeFile(filePath,JSON.stringify(toDos),(err,data)=>{
if(err){
res.status(404);
console.log("Error in writing the file back!!")
}
})
}
}
app.get('/todos',requestPost);
app.get('/todos/:id',requestIdElement);
app.post('/todos',addingElement);
app.put('/todos/:id',modifyingElement);
app.delete('/todos/:id',deleteElement);






//module.exports = app;