Skip to content
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
80 changes: 80 additions & 0 deletions 02-nodejs/authenticationServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
28 changes: 28 additions & 0 deletions 02-nodejs/fileServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 3 additions & 1 deletion 02-nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
109 changes: 108 additions & 1 deletion 02-nodejs/todoServer.js → 02-nodejs/solutions/todoServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
1 change: 1 addition & 0 deletions 02-nodejs/solutions/todos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":6214,"title":"Buy groceries","description":"I should buy groceries"}]