From dc9a2dd895e4a19475652035473718966508bca6 Mon Sep 17 00:00:00 2001 From: Veeky Date: Mon, 8 Jan 2024 23:13:08 +0530 Subject: [PATCH 1/2] whole week 3 --- 02-nodejs/authenticationServer.js | 43 +++++++ 02-nodejs/fileServer.js | 27 +++++ .../solutions/todoServer.solution.file.js | 2 + .../solutions/todoServer.solution.simple.js | 4 +- 02-nodejs/tempCodeRunnerFile.js | 1 + 02-nodejs/todo.json | 1 + 02-nodejs/todoServer.js | 107 +++++++++++++++++- 7 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 02-nodejs/tempCodeRunnerFile.js create mode 100644 02-nodejs/todo.json diff --git a/02-nodejs/authenticationServer.js b/02-nodejs/authenticationServer.js index c5278b94..7fe07b08 100644 --- a/02-nodejs/authenticationServer.js +++ b/02-nodejs/authenticationServer.js @@ -33,5 +33,48 @@ const express = require("express") const PORT = 3000; const app = express(); // write your logic here, DONT WRITE app.listen(3000) when you're running tests, the tests will automatically start the server +const bodyParser = require('body-parser') + +app.use(bodyParser.json()); + +let arr=[] +app.get('/data', (req, res)=>{ + const {username, password} = +}) + +//1 +app.post('/signup', (req, res)=>{ + // const {username, password, firstname, lastname} = req.body; + const newUser={ + username: req.body.username, + password: req.body.password, + fistname: req.body.firstname, + lastname: req.body.lastname + } + const userExist=find(user=>user===newUser); + if(userExist){ + return res.status(400).json({error: "Bad request"}) + }else{ + const Id=Math.floor(Math.random()*1000000); + newUser.push({id:Id}); + return res.status(201).json({"successfull"}) + } +}); + +//2 +app.post('/login', (req, res)=>{ + const {username, password} = req.body; + const user = arr.find(user=>user.username===username); + if(!user || user.password!==password){ + return res.status(401).json({error: 'Unauthorized'}); + } + res.status(200).json({ + id: user.id, + firstName: user.firstName, + lastName: user.lastName + }) +}); + +app.use() module.exports = app; diff --git a/02-nodejs/fileServer.js b/02-nodejs/fileServer.js index 82e7719b..f4a4fc8c 100644 --- a/02-nodejs/fileServer.js +++ b/02-nodejs/fileServer.js @@ -21,5 +21,32 @@ const fs = require('fs'); const path = require('path'); const app = express(); +app.get('/files', (req, res)=>{ + const fileDirectory = path.join(__dirname,'./files/'); + fs.readdir(fileDirectory, (err, data)=>{ + if(err){ + return res.status(500).json({error: "Failed to retrieve files"}); + } + res.status(200).json(data); + }); +}); + +app.get('/file/:filename', (req, res)=>{ + const fileName = req.params.filename; + const fileDirectory = path.join(__dirname, './files/', fileName); + fs.readFile(fileDirectory, "utf8", (err, data)=>{ + if(err){ + return res.status(404).send( 'File not found'); + } + res.send(data); + }); +}); + +app.all('*',(req, res)=>{ + res.status(404).send("Route not found"); +}); module.exports = app; + + + diff --git a/02-nodejs/solutions/todoServer.solution.file.js b/02-nodejs/solutions/todoServer.solution.file.js index 453f56cd..81969db4 100644 --- a/02-nodejs/solutions/todoServer.solution.file.js +++ b/02-nodejs/solutions/todoServer.solution.file.js @@ -103,4 +103,6 @@ app.use((req, res, next) => { res.status(404).send(); }); +// app.listen(3000) + module.exports = app; diff --git a/02-nodejs/solutions/todoServer.solution.simple.js b/02-nodejs/solutions/todoServer.solution.simple.js index 7e96dd22..656a41f4 100644 --- a/02-nodejs/solutions/todoServer.solution.simple.js +++ b/02-nodejs/solutions/todoServer.solution.simple.js @@ -71,4 +71,6 @@ app.use((req, res, next) => { res.status(404).send(); }); -module.exports = app; +// module.exports = app; + +app.listen(3000); \ No newline at end of file diff --git a/02-nodejs/tempCodeRunnerFile.js b/02-nodejs/tempCodeRunnerFile.js new file mode 100644 index 00000000..d67c2cf2 --- /dev/null +++ b/02-nodejs/tempCodeRunnerFile.js @@ -0,0 +1 @@ +npm run test-todoServer \ No newline at end of file diff --git a/02-nodejs/todo.json b/02-nodejs/todo.json new file mode 100644 index 00000000..7911b6b7 --- /dev/null +++ b/02-nodejs/todo.json @@ -0,0 +1 @@ +[{"id":916331,"title":"gym","description":"8to 12PM"},{"id":597864,"title":"gym","description":"222 to 8"},{"id":732970,"title":"gym","description":"8to 12PM"},{"id":897750},{"id":444071},{"id":709983,"title":"gym","description":"4 to 8"},{"id":78693,"title":"gym","description":"4 to 8"},{"id":28951,"title":"gym","description":"4 to 8"},{"id":481301},{"id":721574}] \ No newline at end of file diff --git a/02-nodejs/todoServer.js b/02-nodejs/todoServer.js index cffc7d60..56825a20 100644 --- a/02-nodejs/todoServer.js +++ b/02-nodejs/todoServer.js @@ -39,11 +39,116 @@ Testing the server - run `npm run test-todoServer` command in terminal */ + // npx jest todoServer.js + const express = require('express'); const bodyParser = require('body-parser'); +const fs = require('fs'); const app = express(); app.use(bodyParser.json()); -module.exports = app; + +function findIndex(arr, id){ + for(let i=0; i{ + fs.readFile("D:\\Programming\\Harkirat\\Week-2-Assignments\\02-nodejs\\todo.json", "utf8", (err, data)=>{ + if(err) throw err; + res.json(JSON.parse(data)); + }); +}); + +app.get("/todos/:id", (req, res)=>{ + fs.readFile("D:\\Programming\\Harkirat\\Week-2-Assignments\\02-nodejs\\todo.json", "utf8", (err, data)=>{ + if(err) throw err; + const todos = JSON.parse(data); + const todoIndex = findIndex(todos, parseInt(req.params.id)); + if(todoIndex===-1){ + res.status(404).send(); + }else { + res.json(todos[todoIndex]); + } + }); +}); + +app.post("/todos", (req, res)=>{ + const newTodo={ + id: Math.floor(Math.random()*1000000), + title: req.body.title, + description: req.body.description + }; + fs.readFile("D:\\Programming\\Harkirat\\Week-2-Assignments\\02-nodejs\\todo.json", "utf8", (err, data)=>{ + if(err) throw err; + const todos = JSON.parse(data); + todos.push(newTodo); + fs.writeFile("D:\\Programming\\Harkirat\\Week-2-Assignments\\02-nodejs\\todo.json", JSON.stringify(todos), (err)=>{ + if(err)throw err; + res.status(201).json(newTodo); + }); + }); +}); + +app.put("/todos/:id", (req, res)=>{ + fs.readFile("D:\\Programming\\Harkirat\\Week-2-Assignments\\02-nodejs\\todo.json", "utf8", (err, data)=>{ + if(err) throw err; + else{ + const todos = JSON.parse(data); + const todoIndex = findIndex(todos, parseInt(req.params.id)); + if(todoIndex==-1) + res.status(404).send("Not found"); + else{ + const updatedTodo={ + id: todos[todoIndex].id, + title: req.body.title, + description: req.body.description + }; + todos[todoIndex]=updatedTodo; + fs.writeFile("D:\\Programming\\Harkirat\\Week-2-Assignments\\02-nodejs\\todo.json", JSON.stringify(todos),(err)=>{ + if(err) throw err; + res.status(200).json(updatedTodo); + }); + } + } + }); +}); + +app.delete("/todos/:id", (req, res)=>{ + fs.readFile("D:\\Programming\\Harkirat\\Week-2-Assignments\\02-nodejs\\todo.json", "utf8", (err, data)=>{ + if(err) throw err; + let todos = JSON.parse(data); + const todoIndex = findIndex(todos, parseInt(req.params.id)); + if(todoIndex==-1){ + res.status(404).send(); + }else{ + todos = removeAtIndex(todos, todoIndex); + fs.writeFile("D:\\Programming\\Harkirat\\Week-2-Assignments\\02-nodejs\\todo.json", JSON.stringify(todos), (err)=>{ + if(err) throw err; + res.status(200).json(todos); + }) + } + }) +}) + + +app.use((req, res, next)=>{ + res.status(404).send(); +} ) + +// module.exports = app; + +app.listen(3000); \ No newline at end of file From 233fe20df26d8217e086604723d654ae08200cfb Mon Sep 17 00:00:00 2001 From: Veeky Date: Fri, 12 Jan 2024 03:04:28 +0530 Subject: [PATCH 2/2] Week 3 reconciler --- 02-nodejs/week3_assignment.html | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 02-nodejs/week3_assignment.html diff --git a/02-nodejs/week3_assignment.html b/02-nodejs/week3_assignment.html new file mode 100644 index 00000000..1546f61b --- /dev/null +++ b/02-nodejs/week3_assignment.html @@ -0,0 +1,44 @@ + + + + + + Document + + +
+ + + \ No newline at end of file