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

week-2-assignment submission #319

Open
wants to merge 1 commit 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
69 changes: 69 additions & 0 deletions 02-nodejs/authenticationServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,73 @@ 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 users = [];

app.use(express.json());
app.post("/signup", (req, res) => {
var user = req.body;
let userAlreadyExists = false;
for (var i = 0; i<users.length; i++) {
if (users[i].email === user.email) {
userAlreadyExists = true;
break;
}
}
if (userAlreadyExists) {
res.sendStatus(400);
} else {
users.push(user);
res.status(201).send("Signup successful");
}
});

app.post("/login", (req, res) => {
var user = req.body;
let userFound = null;
for (var i = 0; i<users.length; i++) {
if (users[i].email === user.email && users[i].password === user.password) {
userFound = users[i];
break;
}
}

if (userFound) {
res.json({
firstName: userFound.firstName,
lastName: userFound.lastName,
email: userFound.email
});
} else {
res.sendStatus(401);
}
});

app.get("/data", (req, res) => {
var email = req.headers.email;
var password = req.headers.password;
let userFound = false;
for (var i = 0; i<users.length; i++) {
if (users[i].email === email && users[i].password === password) {
userFound = true;
break;
}
}

if (userFound) {
let usersToReturn = [];
for (let i = 0; i<users.length; i++) {
usersToReturn.push({
firstName: users[i].firstName,
lastName: users[i].lastName,
email: users[i].email
});
}
res.json({
users
});
} else {
res.sendStatus(401);
}
});

module.exports = app;
23 changes: 23 additions & 0 deletions 02-nodejs/fileServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,28 @@ const fs = require('fs');
const path = require('path');
const app = express();

app.get('/files', (req, res) => {
fs.readdir(path.join(__dirname, './files/'), (err, files) => {
if(err) {
return res.status(500).json({error: "Failed to retrieve files"});
}
res.json(files);
});
});

app.get('/file/:filename', (req, res) => {
const filepath = path.join(__dirname, './files/', req.params.filename);

fs.readFile(filepath, 'utf8', (err, data) => {
if(err) {
return res.status(404).send('File not found');
}
res.send(data);
});
});

app.get('*', (req, res) => {
res.status(404).send('Route not found');
});

module.exports = app;
36 changes: 18 additions & 18 deletions 02-nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions 02-nodejs/todoServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,63 @@ const app = express();

app.use(bodyParser.json());

let todos = [];

function findIndex(arr, id) {
for(var i=0; i<arr.length; i++) {
if(arr[i].id === id) return i;
}
return -1;
}
app.get('/todos', (req, res) => {
res.json(todos);
});

app.get('/todos/:id', (req, res) => {
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() * 10000),
title: req.body.title,
description: req.body.description
}
todos.push(newTodo);
res.status(201).json(newTodo);
});

app.put('/todos/:id', (req, res) => {
const todoIndex = findIndex(todos, parseInt(req.params.id));
if(todoIndex == -1) {
res.status(404).send();
}
else {
todos[todoIndex].title = req.body.title,
todos[todoIndex].description = req.body.description,
res.json(todos[todoIndex]);
}
});

app.delete('/todos/:id', (req, res) => {
const todoIndex = findIndex(todos, parseInt(req.params.id));
if(todoIndex == -1) {
res.status(404).send();
}
else {
todos.splice(todoIndex, 1);
res.status(200).send();
}
});

app.use((req, res, next) => {
res.status(404).send();
});

module.exports = app;