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

Completed todo assignment #325

Open
wants to merge 3 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
81 changes: 75 additions & 6 deletions 02-nodejs/authenticationServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

47 changes: 40 additions & 7 deletions 02-nodejs/fileServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
69 changes: 67 additions & 2 deletions 02-nodejs/todoServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
})