From 32b2123d45ba66c8e51593afb715df6de97e982f Mon Sep 17 00:00:00 2001 From: Darwin Date: Thu, 24 Jan 2019 20:15:39 -0600 Subject: [PATCH 1/4] users working --- index.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/index.js b/index.js index 8b13789..c144e36 100644 --- a/index.js +++ b/index.js @@ -1 +1,33 @@ +var http = require('http'); +let users = require("./state").users; +let server = http.createServer(messageReceived); +server.listen(8080); + +function messageReceived(req, res) { + res.writeHead(200, {'Content-Type': 'text/plain'}); + if(req.method === "GET" && req.url.indexOf("/users/") > -1){ + let id = req.url.split("/"); + let user = users.find(p=>p["_id"] == id[2]); + let userJSON = JSON.stringify(user); + res.write(userJSON); + } + else if(req.method === "GET" && req.url === "/users"){ + let usersJSON = JSON.stringify(users); + res.write(usersJSON); + } + else if(req.method === "POST" && req.url === "/users"){ + let body = []; + req.on('data', (chunk) => { + body.push(chunk); + }).on('end', () => { + body = Buffer.concat(body).toString(); + let user = JSON.parse(body); + users.push(user); + }); + } + else{ + res.write("Not Found"); + } + res.end(); +} From 552ee58777a4a06a59dd3155d773756b93f26445 Mon Sep 17 00:00:00 2001 From: Darwin Risser Date: Sat, 26 Jan 2019 16:02:40 -0600 Subject: [PATCH 2/4] users and products working --- index.js | 23 ++++++++++++++++++++++- state.js | 10 +++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index c144e36..c1117ed 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ var http = require('http'); let users = require("./state").users; +let products = require("./state").products; let server = http.createServer(messageReceived); server.listen(8080); @@ -7,14 +8,24 @@ function messageReceived(req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); if(req.method === "GET" && req.url.indexOf("/users/") > -1){ let id = req.url.split("/"); - let user = users.find(p=>p["_id"] == id[2]); + let user = users.find(p=>p["id"] == id[2]); let userJSON = JSON.stringify(user); res.write(userJSON); } + if(req.method === "GET" && req.url.indexOf("/products/") > -1){ + let id = req.url.split("/"); + let product = products.find(p=>p["id"] == id[2]); + let productJSON = JSON.stringify(product); + res.write(productJSON); + } else if(req.method === "GET" && req.url === "/users"){ let usersJSON = JSON.stringify(users); res.write(usersJSON); } + else if(req.method === "GET" && req.url === "/products"){ + let productsJSON = JSON.stringify(products); + res.write(productsJSON); + } else if(req.method === "POST" && req.url === "/users"){ let body = []; req.on('data', (chunk) => { @@ -25,6 +36,16 @@ function messageReceived(req, res) { users.push(user); }); } + else if(req.method === "POST" && req.url === "/products"){ + let body = []; + req.on('data', (chunk) => { + body.push(chunk); + }).on('end', () => { + body = Buffer.concat(body).toString(); + let product = JSON.parse(body); + products.push(product); + }); + } else{ res.write("Not Found"); } diff --git a/state.js b/state.js index ebab277..892d1c3 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" From 56d437688304efbff2d96ee45c5d5e43b7775f09 Mon Sep 17 00:00:00 2001 From: Darwin Risser Date: Sat, 26 Jan 2019 16:46:55 -0600 Subject: [PATCH 3/4] put and delete working for users --- index.js | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index c1117ed..8ffcd3c 100644 --- a/index.js +++ b/index.js @@ -35,17 +35,34 @@ function messageReceived(req, res) { let user = JSON.parse(body); users.push(user); }); - } - else if(req.method === "POST" && req.url === "/products"){ + } + else if(req.method === "POST" && req.url === "/products"){ + let body = []; + req.on('data', (chunk) => { + body.push(chunk); + }).on('end', () => { + body = Buffer.concat(body).toString(); + let product = JSON.parse(body); + products.push(product); + }); + } + else if(req.method === "PUT" && req.url.indexOf("/users/") > -1){ + let id = req.url.split("/"); + let user = users.find(p=>p["id"] == id[2]); let body = []; req.on('data', (chunk) => { body.push(chunk); }).on('end', () => { body = Buffer.concat(body).toString(); - let product = JSON.parse(body); - products.push(product); + body = JSON.parse(body); + user.name = body.name; }); - } + } + else if(req.method === "DELETE" && req.url.indexOf("/users/") > -1){ + let id = req.url.split("/"); + // let user = users.find(p=>p["id"] == id[2]); + users.splice(id-1, 1) + } else{ res.write("Not Found"); } From affcdf0ff1222c7ff44f6eeee57a3dfd18c3dd72 Mon Sep 17 00:00:00 2001 From: Darwin Risser Date: Sat, 26 Jan 2019 16:56:08 -0600 Subject: [PATCH 4/4] new user id increments automatically --- README.md | 2 +- index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 04970ba..5757353 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ A starter repo for the ACA Advanced `My First Web Server` project. ## Part 2. Body * Give your server the ability to handle a POST request with a path "/users" and add the body from the client to the users array -* Assign an _id property to the user object that is a number that increments by 1 each time. +* Assign an id property to the user object that is a number that increments by 1 each time. * Send the newly created user object back to the client ## Part 3. Use path variables diff --git a/index.js b/index.js index 8ffcd3c..bf74a52 100644 --- a/index.js +++ b/index.js @@ -33,6 +33,7 @@ function messageReceived(req, res) { }).on('end', () => { body = Buffer.concat(body).toString(); let user = JSON.parse(body); + user.id = users.length+1; users.push(user); }); } @@ -60,7 +61,6 @@ function messageReceived(req, res) { } else if(req.method === "DELETE" && req.url.indexOf("/users/") > -1){ let id = req.url.split("/"); - // let user = users.find(p=>p["id"] == id[2]); users.splice(id-1, 1) } else{