From f2dd034808465afcd48944e3c0ec43f1785dbf73 Mon Sep 17 00:00:00 2001 From: atinder-pal-singh Date: Tue, 26 Sep 2023 00:55:31 +0530 Subject: [PATCH 1/4] week-2 authenticationServer. js solutions --- 02-nodejs/authenticationServer.js | 80 +++++++++++++++++++++++++++++++ 02-nodejs/package.json | 4 +- 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/02-nodejs/authenticationServer.js b/02-nodejs/authenticationServer.js index c5278b94..ceafecd6 100644 --- a/02-nodejs/authenticationServer.js +++ b/02-nodejs/authenticationServer.js @@ -30,8 +30,88 @@ */ const express = require("express") +const bodyParser = require("body-parser") +const { v4: uuidv4 } = require("uuid"); +const jwt = require("jsonwebtoken"); 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 +var arrUsers = []; +app.use(bodyParser.json()) + +app.post("/signup", (req,res) => { + userExists = false; + var jsonObject = req.body; + if(arrUsers.length > 0) { + arrUsers.forEach((val) => { + if(jsonObject.email === val.email) { + userExists = true; + } + }) + } + + if (!userExists) { + jsonObject.uuid = uuidv4(); + arrUsers.push(jsonObject); + res.status(201).send("Signup successful"); + } else { + res.status(400).send("Username already exists"); + } +}) + +app.post("/login", (req,res) => { + userFound = false; + requestedUser = {}; + var authFields = req.body; + if(arrUsers.length > 0) { + arrUsers.forEach((val) => { + if(val.email === authFields.email && val.password === authFields.password) { + userFound = true; + requestedUser = val; + } + }) + } + + if(userFound) { + var token = jwt.sign({ + username: authFields.username, + password: authFields.password + }, "privatekey"); + res.json({ + "email": requestedUser.email, + "firstName": requestedUser.firstName, + "lastName": requestedUser.lastName, + "token": token + }); + } else { + res.status(401).send("Unauthorized user credentials"); + } + +}) + +app.get("/data", (req,res) => { + var emailD = req.headers.email; + var passwordD = req.headers.password; + var returnObj = []; + if (emailD != null && emailD.trim() != "" && passwordD != null && passwordD.trim() != "") { + if(arrUsers.length > 0) { + arrUsers.forEach((val) => { + returnObj.push({ + "firstName": val.firstName, + "lastName": val.lastName, + "id": val.uuid + }); + }); + } + + res.status(200).send({ + "users": returnObj + }); + } else { + res.status(401).send("Unauthorized"); + } +}) + +app.listen(PORT); module.exports = app; diff --git a/02-nodejs/package.json b/02-nodejs/package.json index 4858530a..9c184456 100644 --- a/02-nodejs/package.json +++ b/02-nodejs/package.json @@ -21,7 +21,9 @@ "testTimeout": 10000 }, "dependencies": { + "body-parser": "^1.20.2", "express": "^4.18.2", - "uuid": "^9.0.0" + "jsonwebtoken": "^9.0.2", + "uuid": "^9.0.1" } } From a5cba43e0334a356febdabd5bb1a116c60a0b289 Mon Sep 17 00:00:00 2001 From: atinder-pal-singh Date: Wed, 27 Sep 2023 00:41:05 +0530 Subject: [PATCH 2/4] added todos commands --- 02-nodejs/authenticationServer.js | 2 +- 02-nodejs/todoServer.js | 70 ++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/02-nodejs/authenticationServer.js b/02-nodejs/authenticationServer.js index ceafecd6..9a1af489 100644 --- a/02-nodejs/authenticationServer.js +++ b/02-nodejs/authenticationServer.js @@ -113,5 +113,5 @@ app.get("/data", (req,res) => { } }) -app.listen(PORT); +// app.listen(PORT); module.exports = app; diff --git a/02-nodejs/todoServer.js b/02-nodejs/todoServer.js index cffc7d60..8ed871d7 100644 --- a/02-nodejs/todoServer.js +++ b/02-nodejs/todoServer.js @@ -41,9 +41,77 @@ */ const express = require('express'); const bodyParser = require('body-parser'); - const app = express(); +var allTodos = []; +const PORT = 3000; app.use(bodyParser.json()); +app.get("/todos", (req,res) => { + res.json(allTodos); +}) + +app.get("/todos/:id", (req,res) => { + var queryId = req.params.id; + var returnObj = {}; + if (allTodos.length > 0) { + allTodos.forEach((val) => { + if (val.id == queryId) { + returnObj = val; + } + }) + } + + if (returnObj.id != null) { + res.json(returnObj); + } else { + res.status(404).send("Not Found"); + } +}) + +app.post("/todos", (req,res) => { + var addTodos = req.body; + var id = allTodos.length + 1; + if (addTodos != null) { + addTodos.id = id; + allTodos.push(addTodos); + res.status(201).send({"id": id}); + } +}) + +app.put("/todos/:id", (req,res) => { + var id = req.params.id; + var updatedItem = req.body; + var index = null; + if(allTodos.length > 0) { + index = allTodos.findIndex((val) => val.id == id); + + if (index > -1) { + updatedItem.id = index + 1; + allTodos[index] = updatedItem; + res.status(200).send("found and updated"); + } + } + if (index == -1) { + res.status(404).send("Not Found"); + } +}) + +app.delete("/todos/:id", (req,res) => { + var id = req.params.id; + var index = null; + if(allTodos.length > 0) { + index = allTodos.findIndex((val) => val.id == id); + + if (index > -1) { + allTodos.splice(index, 1); + res.status(200).send("found and deleted"); + } + } + if (index == -1) { + res.status(404).send("Not Found"); + } +}) + +// app.listen(PORT); module.exports = app; From 8b6befa0111478cfa1919e1a7a54fccdf25418f3 Mon Sep 17 00:00:00 2001 From: atinder-pal-singh Date: Thu, 28 Sep 2023 18:55:41 +0530 Subject: [PATCH 3/4] file Server solution added --- 02-nodejs/fileServer.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/02-nodejs/fileServer.js b/02-nodejs/fileServer.js index 82e7719b..e285b2a2 100644 --- a/02-nodejs/fileServer.js +++ b/02-nodejs/fileServer.js @@ -20,6 +20,34 @@ const express = require('express'); const fs = require('fs'); const path = require('path'); const app = express(); +const PORT = 3000; +app.get("/files", (req,res) => { + fs.readdir(path.join(__dirname, '/files/'), (err, files) => { + if (err) { + res.status(500).send("Cant retrieve files "); + } else { + res.json(files); + } + }); +}) +app.get("/file/:fileName", (req,res) => { + var filepath = path.join(__dirname, './files/', req.params.fileName); + + fs.readFile(filepath, "utf8", (err,data) => { + if (err) { + res.status(404).send("File not found"); + } else { + console.log(data); + res.send(data); + } + }); +}) + +app.all("*", (req,res) => { + res.status(404).send("Route not found"); +}) + +// app.listen(PORT); module.exports = app; From 1219e2fe1f476bbade6ff4576d6c2c0177bd8794 Mon Sep 17 00:00:00 2001 From: atinder-pal-singh Date: Fri, 29 Sep 2023 19:57:03 +0530 Subject: [PATCH 4/4] todos from json file --- 02-nodejs/{ => solutions}/todoServer.js | 119 ++++++++++++++++-------- 02-nodejs/solutions/todos.json | 1 + 2 files changed, 80 insertions(+), 40 deletions(-) rename 02-nodejs/{ => solutions}/todoServer.js (58%) diff --git a/02-nodejs/todoServer.js b/02-nodejs/solutions/todoServer.js similarity index 58% rename from 02-nodejs/todoServer.js rename to 02-nodejs/solutions/todoServer.js index 8ed871d7..ca628031 100644 --- a/02-nodejs/todoServer.js +++ b/02-nodejs/solutions/todoServer.js @@ -41,6 +41,7 @@ */ const express = require('express'); const bodyParser = require('body-parser'); +const fs = require('fs'); const app = express(); var allTodos = []; const PORT = 3000; @@ -48,70 +49,108 @@ const PORT = 3000; app.use(bodyParser.json()); app.get("/todos", (req,res) => { - res.json(allTodos); + fs.readFile("todos.json", "utf-8", (err, data) => { + if (err) throw err; + res.json(JSON.parse(data)); + }) }) app.get("/todos/:id", (req,res) => { var queryId = req.params.id; - var returnObj = {}; - if (allTodos.length > 0) { - allTodos.forEach((val) => { - if (val.id == queryId) { - returnObj = val; + + fs.readFile("todos.json", "utf-8", (err,data) => { + if (err) throw err; + var returnobj = {}; + const todos = JSON.parse(data); + todos.forEach((val) => { + if(val.id == queryId) { + returnobj = val; } }) - } - if (returnObj.id != null) { - res.json(returnObj); - } else { - res.status(404).send("Not Found"); - } + if (returnobj.id != null) { + res.json(returnobj); + } else { + res.status(404).send("Not Found"); + } + }) }) app.post("/todos", (req,res) => { - var addTodos = req.body; - var id = allTodos.length + 1; - if (addTodos != null) { - addTodos.id = id; - allTodos.push(addTodos); - res.status(201).send({"id": id}); + const newtodo = { + id: Math.floor(Math.random() * 10000), + title: req.body.title, + description: req.body.description } + + fs.readFile("todos.json", "utf-8", (err,data) => { + if (err) throw err; + const todos = JSON.parse(data); + todos.push(newtodo); + + fs.writeFile("todos.json", JSON.stringify(todos), (err) => { + if (err) throw err; + res.status(201).send({"id": newtodo.id}); + }) + }) }) app.put("/todos/:id", (req,res) => { var id = req.params.id; var updatedItem = req.body; + updatedItem.id = id; var index = null; - if(allTodos.length > 0) { - index = allTodos.findIndex((val) => val.id == id); - - if (index > -1) { - updatedItem.id = index + 1; - allTodos[index] = updatedItem; - res.status(200).send("found and updated"); + + fs.readFile("todos.json", "utf-8", (err, data) => { + if (err) throw err; + const todos = JSON.parse(data); + + if(todos.length > 0) { + index = todos.findIndex((val) => val.id == id); + + if (index > -1) { + todos[index] = updatedItem; + + fs.writeFile("todos.json", JSON.stringify(todos), (err) => { + if (err) throw err; + res.status(200).send("found and updated"); + }) + } + + if (index == -1) { + res.status(404).send("Not Found"); + } } - } - if (index == -1) { - res.status(404).send("Not Found"); - } + }) }) app.delete("/todos/:id", (req,res) => { var id = req.params.id; var index = null; - if(allTodos.length > 0) { - index = allTodos.findIndex((val) => val.id == id); - - if (index > -1) { - allTodos.splice(index, 1); - res.status(200).send("found and deleted"); + + fs.readFile("todos.json", "utf-8", (err, data) => { + if (err) throw err; + + const todos = JSON.parse(data); + + if(todos.length > 0) { + index = todos.findIndex((val) => val.id == id); + + if (index > -1) { + todos.splice(index, 1); + + fs.writeFile("todos.json", JSON.stringify(todos), (err) => { + if (err) throw err; + res.status(200).send("found and deleted"); + }) + } + + if (index == -1) { + res.status(404).send("Not Found"); + } } - } - if (index == -1) { - res.status(404).send("Not Found"); - } + }) }) -// app.listen(PORT); +app.listen(PORT); module.exports = app; diff --git a/02-nodejs/solutions/todos.json b/02-nodejs/solutions/todos.json index e69de29b..efabb469 100644 --- a/02-nodejs/solutions/todos.json +++ b/02-nodejs/solutions/todos.json @@ -0,0 +1 @@ +[{"id":6214,"title":"Buy groceries","description":"I should buy groceries"}] \ No newline at end of file