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

whole week 3 #336

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
43 changes: 43 additions & 0 deletions 02-nodejs/authenticationServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,48 @@ 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
const bodyParser = require('body-parser')

app.use(bodyParser.json());

let arr=[]
app.get('/data', (req, res)=>{
const {username, password} =
})

//1
app.post('/signup', (req, res)=>{
// const {username, password, firstname, lastname} = req.body;
const newUser={
username: req.body.username,
password: req.body.password,
fistname: req.body.firstname,
lastname: req.body.lastname
}
const userExist=find(user=>user===newUser);
if(userExist){
return res.status(400).json({error: "Bad request"})
}else{
const Id=Math.floor(Math.random()*1000000);
newUser.push({id:Id});
return res.status(201).json({"successfull"})
}
});

//2
app.post('/login', (req, res)=>{
const {username, password} = req.body;
const user = arr.find(user=>user.username===username);
if(!user || user.password!==password){
return res.status(401).json({error: 'Unauthorized'});
}
res.status(200).json({
id: user.id,
firstName: user.firstName,
lastName: user.lastName
})
});

app.use()

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

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

app.get('/file/:filename', (req, res)=>{
const fileName = req.params.filename;
const fileDirectory = path.join(__dirname, './files/', fileName);
fs.readFile(fileDirectory, "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");
});

module.exports = app;



2 changes: 2 additions & 0 deletions 02-nodejs/solutions/todoServer.solution.file.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,6 @@ app.use((req, res, next) => {
res.status(404).send();
});

// app.listen(3000)

module.exports = app;
4 changes: 3 additions & 1 deletion 02-nodejs/solutions/todoServer.solution.simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,6 @@ app.use((req, res, next) => {
res.status(404).send();
});

module.exports = app;
// module.exports = app;

app.listen(3000);
1 change: 1 addition & 0 deletions 02-nodejs/tempCodeRunnerFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npm run test-todoServer
1 change: 1 addition & 0 deletions 02-nodejs/todo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":916331,"title":"gym","description":"8to 12PM"},{"id":597864,"title":"gym","description":"222 to 8"},{"id":732970,"title":"gym","description":"8to 12PM"},{"id":897750},{"id":444071},{"id":709983,"title":"gym","description":"4 to 8"},{"id":78693,"title":"gym","description":"4 to 8"},{"id":28951,"title":"gym","description":"4 to 8"},{"id":481301},{"id":721574}]
107 changes: 106 additions & 1 deletion 02-nodejs/todoServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,116 @@

Testing the server - run `npm run test-todoServer` command in terminal
*/
// npx jest todoServer.js

const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');

const app = express();

app.use(bodyParser.json());

module.exports = app;

function findIndex(arr, id){
for(let i=0; i<arr.length;i++){
if(arr[i].id===id)
return i;
}
return -1;
}

function removeAtIndex(arr, index){
const newArray = [];
for(let i=0;i<arr.length;i++){
if(i!=index)newArray.push(arr[i]);
}
return newArray;
}

app.get("/todos", (req, res)=>{
fs.readFile("D:\\Programming\\Harkirat\\Week-2-Assignments\\02-nodejs\\todo.json", "utf8", (err, data)=>{
if(err) throw err;
res.json(JSON.parse(data));
});
});

app.get("/todos/:id", (req, res)=>{
fs.readFile("D:\\Programming\\Harkirat\\Week-2-Assignments\\02-nodejs\\todo.json", "utf8", (err, data)=>{
if(err) throw err;
const todos = JSON.parse(data);
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()*1000000),
title: req.body.title,
description: req.body.description
};
fs.readFile("D:\\Programming\\Harkirat\\Week-2-Assignments\\02-nodejs\\todo.json", "utf8", (err, data)=>{
if(err) throw err;
const todos = JSON.parse(data);
todos.push(newTodo);
fs.writeFile("D:\\Programming\\Harkirat\\Week-2-Assignments\\02-nodejs\\todo.json", JSON.stringify(todos), (err)=>{
if(err)throw err;
res.status(201).json(newTodo);
});
});
});

app.put("/todos/:id", (req, res)=>{
fs.readFile("D:\\Programming\\Harkirat\\Week-2-Assignments\\02-nodejs\\todo.json", "utf8", (err, data)=>{
if(err) throw err;
else{
const todos = JSON.parse(data);
const todoIndex = findIndex(todos, parseInt(req.params.id));
if(todoIndex==-1)
res.status(404).send("Not found");
else{
const updatedTodo={
id: todos[todoIndex].id,
title: req.body.title,
description: req.body.description
};
todos[todoIndex]=updatedTodo;
fs.writeFile("D:\\Programming\\Harkirat\\Week-2-Assignments\\02-nodejs\\todo.json", JSON.stringify(todos),(err)=>{
if(err) throw err;
res.status(200).json(updatedTodo);
});
}
}
});
});

app.delete("/todos/:id", (req, res)=>{
fs.readFile("D:\\Programming\\Harkirat\\Week-2-Assignments\\02-nodejs\\todo.json", "utf8", (err, data)=>{
if(err) throw err;
let todos = JSON.parse(data);
const todoIndex = findIndex(todos, parseInt(req.params.id));
if(todoIndex==-1){
res.status(404).send();
}else{
todos = removeAtIndex(todos, todoIndex);
fs.writeFile("D:\\Programming\\Harkirat\\Week-2-Assignments\\02-nodejs\\todo.json", JSON.stringify(todos), (err)=>{
if(err) throw err;
res.status(200).json(todos);
})
}
})
})


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

// module.exports = app;

app.listen(3000);
44 changes: 44 additions & 0 deletions 02-nodejs/week3_assignment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="mainArea"></div>
<script>
function createDomElement(data){
var parentElement = document.getElementById("mainArea");
parentElement.innerHTML = "";
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);

}
}
window.setInterval(()=>{
let todos=[];
for(let i=0;i<Math.floor(Math.random()*100);i++){
todos.push({
title:"Go to gym",
description:"Go to gym at 5",
id:i+1
})
}
createDomElement(todos);
},2000)
</script>
</body>
</html>