From 17038bc0ebbdd0bbe3b263440e6beee69dc56eb4 Mon Sep 17 00:00:00 2001 From: DMuttz Date: Sun, 17 Feb 2019 13:39:26 -0600 Subject: [PATCH 1/2] submitting assignment --- index.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/index.js b/index.js index 8b13789..da9cea1 100644 --- a/index.js +++ b/index.js @@ -1 +1,62 @@ +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'}); + +//Give your server the ability to respond to a GET request with a path "/users" and return the users array from state.js + if(req.method === "GET" && req.url === "/users"){ + let usersJSON = JSON.stringify(users); + res.write(usersJSON); + } + +//Give your server the ability to respond to a GET request with a path "/users/1" and return the first user object from the users array from state.js + else if(req.method === "GET" && req.url.indexOf("/users/") > -1){ + let id = req.url.split("/"); + let user = users.find(p=>p["_id"] == id[2]); + let usersJSON = JSON.stringify(user); + res.write(usersJSON); + } + +//Give your server the ability to respond to a POST request with a path "/users" and just add a hard coded user object to the users array from state.js. .json() the last user in the array to send it back to the client. (if you do another GET request you should see this added) + else if(req.method === "POST" && req.url === "/users"){ + postUsers(req, res); + } + +//Give your server the ability to respond to a PUT request with a path "/users/1" and just change any key value on the first user object in the users array in state.js. .json() this user to send it back to the client. + else if(req.method === "PUT" && req.url === "/users/1"){ + let newUserName = users[0].id.s + res.write(newUserName) + } + + else { + res.write("Not Found"); + } + res.end(); +} + + +//FUNCTION TO ALLOW YOU TO POST TO THE USERS OBJECT +function postUsers(req, res){ + let body = []; + req.on('data', (chunk) => { + body.push(chunk); + }).on('end', () => { + body = Buffer.concat(body).toString(); + let userPost = JSON.parse(body); + users.push(userPost); + }); + } + +// function putUsers(req, res){ +// let body = []; +// req.on('data', (chunk) => { +// body.push(chunk); +// }).on('end', () => { +// body = Buffer.concat(body).toString(); +// let userPut = JSON.parse(body); +// users.push(userPut); +// }); +// } From 39b645ca70f20852112df3cf4e2072412cba9430 Mon Sep 17 00:00:00 2001 From: DMuttz Date: Tue, 19 Feb 2019 17:21:12 -0600 Subject: [PATCH 2/2] made more changes --- index.js | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/index.js b/index.js index da9cea1..b30ebdd 100644 --- a/index.js +++ b/index.js @@ -50,13 +50,4 @@ function postUsers(req, res){ }); } -// function putUsers(req, res){ -// let body = []; -// req.on('data', (chunk) => { -// body.push(chunk); -// }).on('end', () => { -// body = Buffer.concat(body).toString(); -// let userPut = JSON.parse(body); -// users.push(userPut); -// }); -// } +