Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1 +1,74 @@
let http = require('http');
let products = require('./state').products
let users = require('./state').users

let dataObjectArray = [{string: 'products', object: products},{string: 'users', object: users}]

let server=()=>{
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
let objectString = getDataObjectString(req.url)
let object = getDataObject(objectString)
if(req.method === "GET" && req.url.indexOf(`/${objectString}/`) > -1){
let objectJSON = splitPath(req.url, object).objectJSON
res.write(objectJSON);
}
else if(req.method === "POST" && req.url.indexOf(`/${objectString}/`) > -1){
let productsJSON = splitPath(req.url, object)
res.write("you wanted to make a product "+ productsJSON)
}
else if(req.method === "PUT" && req.url.indexOf(`/${objectString}/`) > -1){
let objectName = splitPath(req.url, object).objectName
res.write("you wanted to update product "+objectName)
}
else if(req.method === "DELETE" && req.url.indexOf(`/${objectString}/`) > -1){
let objectName = splitPath(req.url, object).objectName
res.write("Deleted "+objectName)
}
else{
res.write("Not Found");
}
res.end();
}).listen(8080);
}

let getDataObjectString =(url)=>{
url = url.split("/")
return url[1]
}

let getDataObject =(string)=>{
string = dataObjectArray.filter(object=> object.string === string)
string = string.length === 0 ? []: string[0].object
return string
}

let productsById =(id, object)=>{
let array = []
for(let i = 0; i<object.length; i++){
if(object[i].id === id){
array.push(object[i])
}
}
return array
}

let splitPath=(string, object)=>{
let id = string.split("/");
id = parseFloat(id[2])
try{
let objectJSON = JSON.stringify(productsById(id, object))
let objectName = JSON.stringify(productsById(id, object[id-1].name))
return {objectJSON, objectName}
}catch(err)
{
let objectJSON = "No Result"
let objectName = "No Result"
return {objectJSON, objectName}
}
}

server()

//this is homework make this work for users and products

12 changes: 6 additions & 6 deletions state.js
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -33,7 +33,7 @@ exports.users = [

exports.products = [
{
"id": 1,
"id": 1,
"name": "Body Luxuries Sweet Lavender Hand Sanitizer",
"description": "Makes your hands clean",
"rating": 2,
Expand Down