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

"done" #318

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
80 changes: 79 additions & 1 deletion 02-nodejs/authenticationServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,86 @@
*/

const express = require("express")
const PORT = 3000;
const PORT = 3007;
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 = []
// console.log(users[0].email);

app.use(express.json());
app.post('/signup' ,(req,res) =>{
var user = req.body;
let userExist = false;
for(var i = 0 ;i < users.length ;i++ ){
if(users[i].email === user.email ){
userExist = true;
break;
}
}
if(userExist){
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) =>{
let email = req.headers.email
let password = req.headers.password
let userFound = false

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



})

app.listen(PORT ,() =>{
console.log(`Server is listening on http://localhost:${PORT}`);
})

module.exports = app;
29 changes: 29 additions & 0 deletions 02-nodejs/fileServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,39 @@

Testing the server - run `npm run test-fileServer` command in terminal
*/

const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();

const PORT = 5001;

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.all('*', (req, res) => {
res.status(404).send('Route not found');
});

app.listen(PORT , (req,res) =>{
console.log(`App is listening on http://localhost:${PORT}`);
})
module.exports = app;
Loading