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
82 changes: 82 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure you know how to use find()

//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();
}




10 changes: 5 additions & 5 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 Down