From 48568acdc311a138b2e117662b94e5ea7bc9b2a8 Mon Sep 17 00:00:00 2001 From: Alan Houston Date: Thu, 24 Jan 2019 20:22:11 -0600 Subject: [PATCH 1/2] web server inital --- index.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/index.js b/index.js index 8b13789..8dfb67f 100644 --- a/index.js +++ b/index.js @@ -1 +1,54 @@ +//my ip 192.168.36.87. +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 === "/users"){ + getUsers(req, res); + } + else if(req.method === "GET" && req.url === "/products"){ + getProducts(req, res); + } + 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 === "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); + } + else if(req.method === "GET" && req.url.indexOf("/products/") > -1){ + let id = req.url.split("/"); + let product = products.find(p=>p["id"] == id[2]); + let productsJSON = JSON.stringify(product); + res.write(productsJSON); + } + + else{ + res.write("Not Found"); + } + res.end(); +} + +function getUsers(req, res){ + let usersJSON = JSON.stringify(users); + res.write(usersJSON); +} +function getProducts(req, res){ + let productsJSON = JSON.stringify(products); + res.write(productsJSON); +} From 63055e0e5275886e49aca214221fa9b7133caeb1 Mon Sep 17 00:00:00 2001 From: Alan Houston Date: Tue, 29 Jan 2019 17:05:03 -0600 Subject: [PATCH 2/2] more thing working, but not complete --- README.md | 22 +++++++++++----------- index.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 04970ba..71497ff 100644 --- a/README.md +++ b/README.md @@ -4,19 +4,19 @@ A starter repo for the ACA Advanced `My First Web Server` project. ## Part 1. -* Give your server the ability to respond to a GET request with a path "/users" and return the users array from state.js -* 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 -* 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) -* 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. -* Give your server the ability to respond to a DELETE request with a path "/users/1" and remove one item from the users array. send() back a messsage "deleted" +*** 1) Give your server the ability to respond to a GET request with a path "/users" and return the users array from state.js +*** 2) 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 +*** 3) 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) +* 4) 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. +* 5) Give your server the ability to respond to a DELETE request with a path "/users/1" and remove one item from the users array. send() back a messsage "deleted" ## 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. -* Send the newly created user object back to the client +* 6) 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 +*** 7) Assign an _id property to the user object that is a number that increments by 1 each time. +* 8) Send the newly created user object back to the client ## Part 3. Use path variables -* Give your server the ability to respond to a GET request with a path `/users/:userId` and return the user object from the users array that has the _id == userId -* Give your server the ability to respond to a PUT request with a path `/users/:userId` and just change any key value on the user object with this _id -* Give your server the ability to respond to a DELETE request with a path `/users/:userId` and find the user with this id from the array. give this user object a new key value isActive:false. send() back a messsage "deleted" +*** 9) Give your server the ability to respond to a GET request with a path `/users/:userId` and return the user object from the users array that has the _id == userId +* 10) Give your server the ability to respond to a PUT request with a path `/users/:userId` and just change any key value on the user object with this _id +* 11) Give your server the ability to respond to a DELETE request with a path `/users/:userId` and find the user with this id from the array. give this user object a new key value isActive:false. send() back a messsage "deleted" diff --git a/index.js b/index.js index 8dfb67f..6af43d7 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ //my ip 192.168.36.87. +//192.168.1.146. var http = require('http'); let users = require("./state").users; let products = require("./state").products; @@ -8,12 +9,28 @@ server.listen(8080); function messageReceived(req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); + //1 if(req.method === "GET" && req.url === "/users"){ getUsers(req, res); } - else if(req.method === "GET" && req.url === "/products"){ - getProducts(req, res); + //2 + else if(req.method === "GET" && req.url.indexOf("/users/") > -1){ + let id = req.url.split("/"); + let user = users.find(p=>p["_id"] == id[2]); + //9 + if(!user){ + let userId = id[2].split(''); + if(userId[0] == ':'){ + let specificUser = users.find(p=>p["_id"] == userId[1]); + let specificUserJSON = JSON.stringify(specificUser); + res.write(specificUserJSON); + } + }else { + let usersJSON = JSON.stringify(user); + res.write(usersJSON); + } } + //3 else if(req.method === "POST" && req.url === "/users"){ let body = []; req.on('data', (chunk) => { @@ -21,23 +38,44 @@ function messageReceived(req, res) { }).on('end', () => { body = Buffer.concat(body).toString(); let user = JSON.parse(body); + //7 user._id = users.length + 1; users.push(user); }); } - else if(req.method === "GET" && req.url.indexOf("/users/") > -1){ + //4 - not right :( + 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 usersJSON = JSON.stringify(user); - res.write(usersJSON); + let body = []; + req.on('data', (chunk) => { + body.push(chunk); + }).on('end', () => { + body = Buffer.concat(body).toString(); + body = JSON.parse(body); + user.newThing = body;//use res.write?? + }); } - else if(req.method === "GET" && req.url.indexOf("/products/") > -1){ + //5 also not working properly :( + else if(req.method === "DELETE" && req.url.indexOf("/users/") > -1){ let id = req.url.split("/"); - let product = products.find(p=>p["id"] == id[2]); - let productsJSON = JSON.stringify(product); - res.write(productsJSON); + console.log(id[2]) + let user = users.find(u=>u["_id"] == id[2]); + let index = users.indexOf(u=>u[_id] == user[_id]) + console.log(index); + users.splice(Number(index), 1); + res.write('Deleted') } + // else if(req.method === "GET" && req.url === "/products"){ + // getProducts(req, res); + // } + // else if(req.method === "PUT" && req.url.indexOf("/products/") > -1){ + // let id = req.url.split("/"); + // let product = products.find(p=>p["id"] == id[2]); + // let productsJSON = JSON.stringify(product); + // res.write(productsJSON); + // } else{ res.write("Not Found"); }