From cebcd3412e07c42ec2fafdac9ef8f531ee8ce45f Mon Sep 17 00:00:00 2001 From: Nicolai A Date: Mon, 29 Apr 2019 15:39:37 -0500 Subject: [PATCH 1/4] first node server commit --- index.js | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/index.js b/index.js index 8b13789..1a93287 100644 --- a/index.js +++ b/index.js @@ -1 +1,98 @@ +var http = require('http'); +let state = require("./state.js") +let users = state.users +let products = state.products + +let server = http.createServer(function (req, res) { + res.writeHead(200, {'Content-Type': 'text/html'}); + if (req.method === "GET" && req.url === "/users") { + let usersJSON = JSON.stringify(users) + res.write(usersJSON) + } + 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 === "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); + let lastIndex = users.length - 1 + user._id = users[lastIndex]._id + 1 + users.push(user); + }) + } + if (req.method === "PUT" && req.url.indexOf("/users/") > -1) { + let body = []; + let id = req.url.split("/") + req.on('data', (chunk) => { + body.push(chunk); + }).on('end', () => { + body = Buffer.concat(body).toString(); + let user = JSON.parse(body); + let index = users.indexOf(users.find(p=>p["_id"] == id[2])) + users[index] = user + }); + } + if (req.method === "DELETE" && req.url.indexOf("/users/") > -1) { + let id = req.url.split("/") + let user = users.find(p=>p["_id"] == id[2]); + user.isActive = false + //let index = users.indexOf(user) + //users.splice(index, 1) + res.write("deleted") + }; + + if (req.method === "GET" && req.url === "/products") { + let productsJSON = JSON.stringify(products) + res.write(productsJSON) + } + 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) + } + 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); + let lastIndex = products.length - 1 + product._id = products[lastIndex]._id + 1 + products.push(product); + }) + } + if (req.method === "PUT" && req.url.indexOf("/products/") > -1) { + let body = []; + let id = req.url.split("/") + req.on('data', (chunk) => { + body.push(chunk); + }).on('end', () => { + body = Buffer.concat(body).toString(); + let product = JSON.parse(body); + let index = products.indexOf(products.find(p=>p["_id"] == id[2])) + products[index] = product + }); + } + if (req.method === "DELETE" && req.url.indexOf("/products/") > -1) { + let id = req.url.split("/") + let product = products.find(p=>p["_id"] == id[2]); + product.isActive = false + //let index = products.indexOf(product) + //products.splice(index, 1) + res.write("deleted") + }; + +res.end(); +}); + +server.listen(8080) \ No newline at end of file From 87764a2503cee78e493d4032245d4fd8b16112bf Mon Sep 17 00:00:00 2001 From: Nicolai A Date: Mon, 29 Apr 2019 15:42:56 -0500 Subject: [PATCH 2/4] first updatE --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 1a93287..51551c9 100644 --- a/index.js +++ b/index.js @@ -23,9 +23,9 @@ let server = http.createServer(function (req, res) { }).on('end', () => { body = Buffer.concat(body).toString(); let user = JSON.parse(body); - let lastIndex = users.length - 1 - user._id = users[lastIndex]._id + 1 + user._id = users[users.length - 1]._id + 1 users.push(user); + res.write(user) }) } if (req.method === "PUT" && req.url.indexOf("/users/") > -1) { From e53225fc88e17722f7eca6dbed35ee793dcb7c5f Mon Sep 17 00:00:00 2001 From: Nicolai A Date: Mon, 29 Apr 2019 18:21:35 -0500 Subject: [PATCH 3/4] added stringify to post object --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 51551c9..d4f330f 100644 --- a/index.js +++ b/index.js @@ -25,6 +25,7 @@ let server = http.createServer(function (req, res) { let user = JSON.parse(body); user._id = users[users.length - 1]._id + 1 users.push(user); + user = JSON.stringify(user) res.write(user) }) } From 12f7fa6f8178ffa12aa2911846f47ca0fc43d6d5 Mon Sep 17 00:00:00 2001 From: Nicolai A Date: Mon, 29 Apr 2019 21:00:03 -0500 Subject: [PATCH 4/4] fixed PUT and POST methods, allowing them to return the new objects to clients --- index.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index d4f330f..5edb570 100644 --- a/index.js +++ b/index.js @@ -9,12 +9,14 @@ let server = http.createServer(function (req, res) { if (req.method === "GET" && req.url === "/users") { let usersJSON = JSON.stringify(users) res.write(usersJSON) + res.end(); } 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) + res.end(); } if (req.method === "POST" && req.url === "/users") { let body = []; @@ -27,7 +29,8 @@ let server = http.createServer(function (req, res) { users.push(user); user = JSON.stringify(user) res.write(user) - }) + res.end(); + }) } if (req.method === "PUT" && req.url.indexOf("/users/") > -1) { let body = []; @@ -38,7 +41,10 @@ let server = http.createServer(function (req, res) { body = Buffer.concat(body).toString(); let user = JSON.parse(body); let index = users.indexOf(users.find(p=>p["_id"] == id[2])) + user._id = id[2] users[index] = user + res.write(JSON.stringify(user)) + res.end(); }); } if (req.method === "DELETE" && req.url.indexOf("/users/") > -1) { @@ -48,17 +54,19 @@ let server = http.createServer(function (req, res) { //let index = users.indexOf(user) //users.splice(index, 1) res.write("deleted") - }; - + res.end(); + } if (req.method === "GET" && req.url === "/products") { let productsJSON = JSON.stringify(products) res.write(productsJSON) + res.end(); } 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) + res.end(); } if (req.method === "POST" && req.url === "/products") { let body = []; @@ -70,7 +78,10 @@ let server = http.createServer(function (req, res) { let lastIndex = products.length - 1 product._id = products[lastIndex]._id + 1 products.push(product); - }) + product = JSON.stringify(product) + res.write(product) + res.end(); + }); } if (req.method === "PUT" && req.url.indexOf("/products/") > -1) { let body = []; @@ -81,7 +92,10 @@ let server = http.createServer(function (req, res) { body = Buffer.concat(body).toString(); let product = JSON.parse(body); let index = products.indexOf(products.find(p=>p["_id"] == id[2])) + product._id = id[2] products[index] = product + res.write(JSON.stringify(product)) + res.end(); }); } if (req.method === "DELETE" && req.url.indexOf("/products/") > -1) { @@ -91,9 +105,9 @@ let server = http.createServer(function (req, res) { //let index = products.indexOf(product) //products.splice(index, 1) res.write("deleted") + res.end(); }; -res.end(); }); server.listen(8080) \ No newline at end of file