From 283e267277af8824c6b7edd359e8dad3c4cb7229 Mon Sep 17 00:00:00 2001 From: Collin Betori Date: Sun, 28 Apr 2019 14:00:27 -0500 Subject: [PATCH 1/3] upload initial server --- index.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ state.js | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 8b13789..5dbc692 100644 --- a/index.js +++ b/index.js @@ -1 +1,46 @@ +let http = require('http'); +let products = require('./state').products +let users = require('./state').users + +http.createServer(function (req, res) { + res.writeHead(200, {'Content-Type': 'text/html'}); + if(req.method === "GET" && req.url.indexOf("/products/") > -1){ + let productsJSON = splitPath(req.url) + res.write(productsJSON); + } + else if(req.method === "POST" && req.url.indexOf("/products/") > -1){ + res.write("you wanted to make a product") + } + else if(req.method === "PUT" && req.url.indexOf("/products/") > -1){ + res.write("you wanted to update a product") + } + else if(req.method === "DELETE" && req.url.indexOf("/products/") > -1){ + res.write("you wanted to delete a product") + } + else{ + res.write("Not Found"); + } + res.end(); +}).listen(8080); + +let productsById =(id)=>{ + let array = [] + for(let i = 0; i{ + let id = string.split("/"); + id = parseFloat(id[2]) + //let product = products.filter=(product)=>{return product.id === id[2]} + //let productsJSON = JSON.stringify(product(products)); + let productsJSON = JSON.stringify(productsById(id)) + return productsJSON +} +//this is homework make this work for users and products diff --git a/state.js b/state.js index ebab277..4504205 100644 --- a/state.js +++ b/state.js @@ -33,7 +33,7 @@ exports.users = [ exports.products = [ { - "id": 1, + "id": 1, "name": "Body Luxuries Sweet Lavender Hand Sanitizer", "description": "Makes your hands clean", "rating": 2, From 2abeb061ced6f7d79525d7a4388e3fcada8de0b9 Mon Sep 17 00:00:00 2001 From: Collin Betori Date: Sun, 28 Apr 2019 23:23:46 -0500 Subject: [PATCH 2/3] updated server --- index.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 5dbc692..eb24b51 100644 --- a/index.js +++ b/index.js @@ -5,17 +5,20 @@ let users = require('./state').users http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); if(req.method === "GET" && req.url.indexOf("/products/") > -1){ - let productsJSON = splitPath(req.url) + let productsJSON = splitPath(req.url).productsJSON res.write(productsJSON); } else if(req.method === "POST" && req.url.indexOf("/products/") > -1){ + let productsJSON = splitPath(req.url) res.write("you wanted to make a product") } else if(req.method === "PUT" && req.url.indexOf("/products/") > -1){ - res.write("you wanted to update a product") + let productName = splitPath(req.url).productName + res.write("you wanted to update product "+productName) } else if(req.method === "DELETE" && req.url.indexOf("/products/") > -1){ - res.write("you wanted to delete a product") + let productName = splitPath(req.url).productName + res.write("you wanted to delete product "+productName) } else{ res.write("Not Found"); @@ -26,7 +29,6 @@ http.createServer(function (req, res) { let productsById =(id)=>{ let array = [] for(let i = 0; i{ let splitPath=(string)=>{ let id = string.split("/"); id = parseFloat(id[2]) - //let product = products.filter=(product)=>{return product.id === id[2]} - //let productsJSON = JSON.stringify(product(products)); let productsJSON = JSON.stringify(productsById(id)) - return productsJSON + let productName = JSON.stringify(productsById(id)[0].name) + return {productsJSON, productName} +} + +let handleDelete=()=>{ + } //this is homework make this work for users and products From 789f8c62868e2fbde271519b12463926b4e3e185 Mon Sep 17 00:00:00 2001 From: Collin Betori Date: Tue, 30 Apr 2019 17:27:07 -0500 Subject: [PATCH 3/3] flexible code to allow for different obects --- index.js | 89 +++++++++++++++++++++++++++++++++++--------------------- state.js | 10 +++---- 2 files changed, 61 insertions(+), 38 deletions(-) diff --git a/index.js b/index.js index eb24b51..8809890 100644 --- a/index.js +++ b/index.js @@ -2,50 +2,73 @@ let http = require('http'); let products = require('./state').products let users = require('./state').users -http.createServer(function (req, res) { - res.writeHead(200, {'Content-Type': 'text/html'}); - if(req.method === "GET" && req.url.indexOf("/products/") > -1){ - let productsJSON = splitPath(req.url).productsJSON - res.write(productsJSON); - } - else if(req.method === "POST" && req.url.indexOf("/products/") > -1){ - let productsJSON = splitPath(req.url) - res.write("you wanted to make a product") - } - else if(req.method === "PUT" && req.url.indexOf("/products/") > -1){ - let productName = splitPath(req.url).productName - res.write("you wanted to update product "+productName) - } - else if(req.method === "DELETE" && req.url.indexOf("/products/") > -1){ - let productName = splitPath(req.url).productName - res.write("you wanted to delete product "+productName) - } - else{ - res.write("Not Found"); - } - res.end(); -}).listen(8080); +let dataObjectArray = [{string: 'products', object: products},{string: 'users', object: users}] + +let server=()=>{ + http.createServer(function (req, res) { + res.writeHead(200, {'Content-Type': 'text/html'}); + let objectString = getDataObjectString(req.url) + let object = getDataObject(objectString) + if(req.method === "GET" && req.url.indexOf(`/${objectString}/`) > -1){ + let objectJSON = splitPath(req.url, object).objectJSON + res.write(objectJSON); + } + else if(req.method === "POST" && req.url.indexOf(`/${objectString}/`) > -1){ + let productsJSON = splitPath(req.url, object) + res.write("you wanted to make a product "+ productsJSON) + } + else if(req.method === "PUT" && req.url.indexOf(`/${objectString}/`) > -1){ + let objectName = splitPath(req.url, object).objectName + res.write("you wanted to update product "+objectName) + } + else if(req.method === "DELETE" && req.url.indexOf(`/${objectString}/`) > -1){ + let objectName = splitPath(req.url, object).objectName + res.write("Deleted "+objectName) + } + else{ + res.write("Not Found"); + } + res.end(); + }).listen(8080); +} + +let getDataObjectString =(url)=>{ + url = url.split("/") + return url[1] +} -let productsById =(id)=>{ +let getDataObject =(string)=>{ + string = dataObjectArray.filter(object=> object.string === string) + string = string.length === 0 ? []: string[0].object + return string +} + +let productsById =(id, object)=>{ let array = [] - for(let i = 0; i{ +let splitPath=(string, object)=>{ let id = string.split("/"); id = parseFloat(id[2]) - let productsJSON = JSON.stringify(productsById(id)) - let productName = JSON.stringify(productsById(id)[0].name) - return {productsJSON, productName} + try{ + let objectJSON = JSON.stringify(productsById(id, object)) + let objectName = JSON.stringify(productsById(id, object[id-1].name)) + return {objectJSON, objectName} + }catch(err) + { + let objectJSON = "No Result" + let objectName = "No Result" + return {objectJSON, objectName} + } } -let handleDelete=()=>{ +server() -} //this is homework make this work for users and products diff --git a/state.js b/state.js index 4504205..2830eba 100644 --- a/state.js +++ b/state.js @@ -1,30 +1,30 @@ exports.users = [ { - "_id": 1, + "id": 1, "name": "Dale Cooper", "occupation": "FBI Agent", "avatar": "https://upload.wikimedia.org/wikipedia/en/5/50/Agentdalecooper.jpg" }, { - "_id": 2, + "id": 2, "name": "Spike Spiegel", "occupation": "Bounty Hunter", "avatar": "http://vignette4.wikia.nocookie.net/deadliestfiction/images/d/de/Spike_Spiegel_by_aleztron.jpg/revision/latest?cb=20130920231337" }, { - "_id": 3, + "id": 3, "name": "Wirt", "occupation": "adventurer", "avatar": "http://66.media.tumblr.com/5ea59634756e3d7c162da2ef80655a39/tumblr_nvasf1WvQ61ufbniio1_400.jpg" }, { - "_id": 4, + "id": 4, "name": "Michael Myers", "occupation": "Loving little brother", "avatar": "http://vignette2.wikia.nocookie.net/villains/images/e/e3/MMH.jpg/revision/latest?cb=20150810215746" }, { - "_id": 5, + "id": 5, "name": "Dana Scully", "occupation": "FBI Agent", "avatar": "https://pbs.twimg.com/profile_images/718881904834056192/WnMTb__R.jpg"