From b4fdd80550ffce296e213f182735b46c8e85e96d Mon Sep 17 00:00:00 2001 From: Junaid Nawaz Date: Tue, 17 Oct 2023 03:09:36 +0530 Subject: [PATCH 1/3] Update todoServer.js --- 02-nodejs/todoServer.js | 69 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/02-nodejs/todoServer.js b/02-nodejs/todoServer.js index cffc7d60..95865488 100644 --- a/02-nodejs/todoServer.js +++ b/02-nodejs/todoServer.js @@ -41,9 +41,74 @@ */ const express = require('express'); const bodyParser = require('body-parser'); - +const port = 3000 const app = express(); app.use(bodyParser.json()); -module.exports = app; + + +let arr = [] + +app.get("/todos", (req, res) => { + res.status(200).json(arr) +}) + +app.get("/todos/:id", (req, res) => { + const todo = arr.find( i => i.id === parseInt(req.params.id) ) + if(!todo){ + res.status(404).json({ + message: "Error no todo found" + }) + } else { + res.json(todo) + } +}) + + +app.post("/todos", (req, res) => { + const obj = { + id: Math.floor(Math.random() * 1000000), + title: req.body.title, + description: req.body.description + } + arr.push(obj); + res.status(200).json(obj) +}) + +app.put("/todos/:id", (req, res) => { + const todoid = arr.findIndex(i => i.id === parseInt(req.params.id)) + if(todoid === -1){ + res.status(400).json({ + message: "Error no such todo found" + }) + } else { + arr[todoid].title = req.body.title + arr[todoid].description = req.body.description + res.json(arr[todoid]) + } +}) + +app.delete("/todos/:id", (req, res) => { + const todoIndex = arr.findIndex(i => i.id === req.params.id) + if(!todoIndex) { + res.status(400).json({ + message: "Error Couldnt find the todo from list" + }) + } + arr.splice(todoIndex, 1) + res.status(200).json(arr) +}) + +app.use((req, res, next) => { + res.status(404).send(); +}); + +app.listen(port, () => { + console.log(`Example app listening on port ${port}`) +}) + + + + + From 71f759e916f1d01ab4203ea663d05e16403bf2d7 Mon Sep 17 00:00:00 2001 From: jjnawaaz Date: Mon, 26 Aug 2024 22:04:07 +0530 Subject: [PATCH 2/3] Added week 2 assignments --- 02-nodejs/authenticationServer.js | 81 ++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 6 deletions(-) diff --git a/02-nodejs/authenticationServer.js b/02-nodejs/authenticationServer.js index c5278b94..255caf9d 100644 --- a/02-nodejs/authenticationServer.js +++ b/02-nodejs/authenticationServer.js @@ -29,9 +29,78 @@ Testing the server - run `npm run test-authenticationServer` command in terminal */ -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 - -module.exports = app; + 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 + app.use(express.json()) + + + let users = [] + + app.get('/',(req,res)=>{ + res.send("Hello Backend") + }) + + + app.post('/signup',(req,res)=>{ + let data = req.body + let { email , password, firstname, lastname} = data + if(!email || !password || !firstname || !lastname) return res.status(400).send("Please Enter All Fields") + if(data){ + + for(let i = 0; i < users.length; i++){ + + if(users[i].email == email){ + return res.status(401).send("User Already Exists") + } + } + + users.push(data) + res.status(200).send("Successfully signed up") + } else { + res.status(400).send("Error Please send all fields") + } + }) + + app.post('/login',(req,res)=>{ + let data = req.body + let {email,password} = data + + + + + for(let i = 0; i < users.length; i++){ + if(users[i].email === email && users[i].password == password){ + return res.status(200).send("Successfully Logged In") + } else { + return res.status(400).send("Invalid Credentials") + } + } + }) + + app.get('/data',(req,res)=>{ + for(let i = 0; i < users.length; i++){ + console.log(users[i]) + } + + res.status(200).json({ + users + }) + }) + + + + + + app.listen(PORT,()=>{ + console.log(`App started in port ${PORT}`) + }) + + + + + + + module.exports = app; + From 640a191ffb957ef66d3d9a4ab25318dd75b10886 Mon Sep 17 00:00:00 2001 From: jjnawaaz Date: Mon, 26 Aug 2024 22:05:28 +0530 Subject: [PATCH 3/3] Added week 2 Assignment 2 --- 02-nodejs/fileServer.js | 47 +++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/02-nodejs/fileServer.js b/02-nodejs/fileServer.js index 82e7719b..278cf827 100644 --- a/02-nodejs/fileServer.js +++ b/02-nodejs/fileServer.js @@ -16,10 +16,43 @@ 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(); - - -module.exports = app; + const express = require('express'); + const fs = require('fs'); + const path = require('path'); + const app = express(); + + + const files = path.join(__dirname,"files") + const alldata = [] + fs.readdir(files,(err,data)=>{ + if(err){ + console.log(err) + } + alldata.push(data) + + }) + + + app.get('/files',(req,res)=>{ + res.status(200).send(alldata) + + }) + + + + + app.get('/files/:filename',(req,res)=>{ + let data = req.params.filename + fs.readFile(`${files}/${data}`,'utf-8',(err,data)=>{ + if(err) res.status(400).send("Error While Reading") + res.status(200).send(data) + }) + + + }) + + app.listen(3000,(req,res)=>{ + console.log("Server Started at port 3000") + }) + + module.exports = app;