From 97cdd8df9e0463e9e6acea5ee74f747899fff686 Mon Sep 17 00:00:00 2001 From: melfu Date: Tue, 29 Jan 2019 17:39:19 -0600 Subject: [PATCH] mel fuechec --- index.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ state.js | 10 +++---- 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 8b13789..730d625 100644 --- a/index.js +++ b/index.js @@ -1 +1,83 @@ +//import express from "express"; +var http = require('http'); +var users = require("./state").users; +var products = require("./state").products; +var 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"){ + let usersJSON = JSON.stringify(users); //must turn object into string for http + res.write(usersJSON) + } + else if(req.method === "GET" && req.url.indexOf("/users") > -1) { + let id = req.url.split("/"); + let user = users.find(p=>p["id"] === Number(id[2])); + let userJSON = JSON.stringify(user); + res.write(userJSON) + } + if(req.method === "GET" && req.url === "/products"){ + let productsJSON = JSON.stringify(products); //must turn object into string for http + res.write(productsJSON) + } + else if(req.method === "GET" && req.url.indexOf("/products") > -1) { + let id = req.url.split("/"); + let product = products.find(p=>p["id"] === Number(id[2])); + let productJSON = JSON.stringify(product); + res.write(productJSON) + } + + 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 === "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 body = []; + req.on('data', (chunk) => { + body.push(chunk); + }).on('end', () => { + body = Buffer.concat(body).toString(); + let user = JSON.parse(body); + let id = req.url.split("/"); + users.find(p=>p["id"] === Number(id[2])); + //users[Number(id[2])] = user; + users.splice(Number(id[2]), 1, user); + }); + } + + else if(req.method === "DELETE" && req.url.indexOf("/users") > -1){ +// let body = []; +// req.on('data', (chunk) => { +// body.push(chunk); +// }).on('end', () => { +// body = Buffer.concat(body).toString(); + let id = req.url.split("/"); + users.find(p=>p["id"] === Number(id[2])); + users.splice(Number(id[2]), 1); + res.write("deleted"); + } +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"