diff --git a/02-nodejs/authenticationServer.js b/02-nodejs/authenticationServer.js index c5278b94..9a1af489 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/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; 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" } } diff --git a/02-nodejs/todoServer.js b/02-nodejs/solutions/todoServer.js similarity index 50% rename from 02-nodejs/todoServer.js rename to 02-nodejs/solutions/todoServer.js index cffc7d60..ca628031 100644 --- a/02-nodejs/todoServer.js +++ b/02-nodejs/solutions/todoServer.js @@ -41,9 +41,116 @@ */ const express = require('express'); const bodyParser = require('body-parser'); - +const fs = require('fs'); const app = express(); +var allTodos = []; +const PORT = 3000; app.use(bodyParser.json()); +app.get("/todos", (req,res) => { + 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; + + 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"); + } + }) +}) + +app.post("/todos", (req,res) => { + 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; + + 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"); + } + } + }) +}) + +app.delete("/todos/:id", (req,res) => { + var id = req.params.id; + var index = null; + + 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"); + } + } + }) +}) + +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