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 8b13789..bf74a52 100644 --- a/index.js +++ b/index.js @@ -1 +1,71 @@ +var http = require('http'); +let users = require("./state").users; +let products = require("./state").products; +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); + } + 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) => { + body.push(chunk); + }).on('end', () => { + body = Buffer.concat(body).toString(); + let user = JSON.parse(body); + user.id = users.length+1; + 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 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(); + body = JSON.parse(body); + user.name = body.name; + }); + } + else if(req.method === "DELETE" && req.url.indexOf("/users/") > -1){ + let id = req.url.split("/"); + users.splice(id-1, 1) + } + else{ + res.write("Not Found"); + } + res.end(); +} 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"