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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A starter repo for the ACA Advanced `My First Web Server` project.

## 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.
* 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

## Part 3. Use path variables
Expand Down
70 changes: 70 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1 +1,71 @@
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.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 === "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);
}
else if(req.method === "GET" && req.url === "/users"){
let usersJSON = JSON.stringify(users);
res.write(usersJSON);
}
else if(req.method === "GET" && req.url === "/products"){
let productsJSON = JSON.stringify(products);
res.write(productsJSON);
}
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 === "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 id = req.url.split("/");
let user = users.find(p=>p["id"] == id[2]);
let body = [];
req.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
body = JSON.parse(body);
user.name = body.name;
});
}
else if(req.method === "DELETE" && req.url.indexOf("/users/") > -1){
let id = req.url.split("/");
users.splice(id-1, 1)
}
else{
res.write("Not Found");
}
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