From 7ee4c988cc1608f24b3322fc18cf0c65cf23110d Mon Sep 17 00:00:00 2001 From: Carla Wilson Date: Sat, 26 Jan 2019 15:31:49 -0600 Subject: [PATCH 1/2] first commit --- index.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/index.js b/index.js index 8b13789..6d3ab6d 100644 --- a/index.js +++ b/index.js @@ -1 +1,52 @@ + 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 product = products.find(p=>p["_id"] == id[2]); + let usersJSON = JSON.stringify(user); + let productsJSON = JSON.stringify(product); + res.write(usersJSON); + res.write(productsJSON); + + + } + + 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 if(req.method === "PUT" && req.url === "/users/"){ + res.write("you wanted to update a user") + } + + else if(req.method === "DELETE" && req.url === "/users/"){ + res.write("you wanted to delete a user") + } + + else{ + res.write("404 Not Found"); + } + res.end(); + } \ No newline at end of file From b8038e88581d90263c73af900becd1fd258e3dbf Mon Sep 17 00:00:00 2001 From: Carla Wilson Date: Tue, 29 Jan 2019 17:26:18 -0600 Subject: [PATCH 2/2] first commit --- index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 6d3ab6d..0e42649 100644 --- a/index.js +++ b/index.js @@ -11,13 +11,13 @@ if(req.method === "GET" && req.url.indexOf("/users/") > -1){ - let id = req.url.split("/"); - let user = users.find(p=>p["_id"] == id[2]); - let product = products.find(p=>p["_id"] == id[2]); - let usersJSON = JSON.stringify(user); - let productsJSON = JSON.stringify(product); - res.write(usersJSON); - res.write(productsJSON); + let id = req.url.split("/"); + let user = users.find(p=>p["_id"] == id[2]); + let product = products.find(p=>p["_id"] == id[2]); + let usersJSON = JSON.stringify(user); + let productsJSON = JSON.stringify(product); + res.write(usersJSON); + res.write(productsJSON); }