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

wrapup of week 2 #329

Open
wants to merge 2 commits 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
64 changes: 64 additions & 0 deletions 02-nodejs/authenticationServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,69 @@ const express = require("express")
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.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===users.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 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);
}
});

module.exports = app;
24 changes: 24 additions & 0 deletions 02-nodejs/fileServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,29 @@ 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 reterive files. '});
}
res.json(files)
});
});

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

fs.readFile(filepath,'utf-8',(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');
});


module.exports = app;
125 changes: 125 additions & 0 deletions 02-nodejs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<head>
<title>
TODO app
</title>
</head>


<script>
function deleteDone(){
console.log("done deleting");
}
function deleteTodo(id){
console.log(id);
fetch ("http://localhost:3000/todos/"+ id,{

//method:"GET"
method:"DELETE",
headers:{
"Content-Type":"application/json"
}
}).then(deleteDone)
}

function TodosCallback(data){
console.log(data);
var parentElement=document.getElementById("mainArea");
// parentElement.innerHTML=JSON.stringify(data);//insert them into html hora h
for(var i =0; i<data.length;i++){
var childElement=document.createElement("div");

var grandChildElement1=document.createElement("span");
grandChildElement1.innerHTML=data[i].title;

var grandChildElement2=document.createElement("span");
grandChildElement2.innerHTML=data[i].description

var grandChildElement3=document.createElement("button");
grandChildElement3.innerHTML="Delete";
grandChildElement3.setAttribute("onClick","deleteTodo(" + data[i].id +")");


childElement.appendChild(grandChildElement1)
childElement.appendChild(grandChildElement2)
childElement.appendChild(grandChildElement3)


parentElement.appendChild(childElement);
}

}
function getdataCallback(resp){
resp.json().then(TodosCallback);
}
function getdata(){
fetch("http://localhost:3000/todos",{
method:"GET",
}).then(getdataCallback);
}

getdata();


function parsedResponse (data){
console.log(data);
var parentElement=document.getElementById("mainArea");


var childElement=document.createElement("div");

var grandChildElement1=document.createElement("span");
grandChildElement1.innerHTML=data.title;

var grandChildElement2=document.createElement("span");
grandChildElement2.innerHTML=data.description

var grandChildElement3=document.createElement("button");
grandChildElement3.innerHTML="Delete"


childElement.appendChild(grandChildElement1)
childElement.appendChild(grandChildElement2)
childElement.appendChild(grandChildElement3)


parentElement.appendChild(childElement);

}
function callback(resp){
resp.json().then(parsedResponse);
}
function onPress(){
var title=document.getElementById("title").value;//fetching the value which is inside in (browser title page of html.)
var description=document.getElementById("description").value;//fetched itt

fetch ("http://localhost:3000/todos",{
//method:"GET"
method:"POST",
body:JSON.stringify({
title:title,
description:description
}),
headers:{
"Content-Type":"application/json"
}
}).then(callback)
}
</script>


<body>
Todo title
<input type="text" id="title"></input>
<br>
Todo Description
<input type="text" id="description"></input>
<br>
<button onclick="onPress()"> send todo </button>
<!--how do you add js variables into html ?????????????????-->
<div id="mainArea">
hi there!
<!--now go to in your callback where you are logging the data, rather than logging write something like this... var parent element = document -->
</div>
</body>


Loading