From 27d468cefd8599f8e3b1acee50aed300f7afaad8 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Mon, 29 Apr 2019 15:15:28 -0500 Subject: [PATCH 1/4] got users GET request working --- index.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/index.js b/index.js index 8b13789..cc2ae5e 100644 --- a/index.js +++ b/index.js @@ -1 +1,37 @@ +let http = require('http'); +let state = require('./state'); +let users = state.users; +let products = 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' ) { + res.write( JSON.stringify(users) ); + } + + else if( req.method === 'GET' && req.url.indexOf('/users') > -1 ) { + let id = req.url.split('/'); + let user = users.find( u => u['_id'] === Number(id[2]) ); + if(!user) { + res.write('User Not Found'); + } + else { + let userJSON = JSON.stringify(user); + res.write(userJSON); + } + } + + else { + res.write('Not Found') + } + + if( req.method === 'POST' && req.url === '/users' ) { + + } + + res.end(); +} From 51845e9fba683f0e73d2022e5070542a0b9ebdd0 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Tue, 30 Apr 2019 18:17:01 -0500 Subject: [PATCH 2/4] started. got users GETs, POST and PUT going --- index.js | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index cc2ae5e..ee2575d 100644 --- a/index.js +++ b/index.js @@ -9,10 +9,13 @@ server.listen(8080); function messageReceived(req, res) { res.writeHead( 200, {'Content-Type': 'text/plain'} ); + // get all users: if( req.method === 'GET' && req.url === '/users' ) { res.write( JSON.stringify(users) ); + res.end() } + // get specific user by id: else if( req.method === 'GET' && req.url.indexOf('/users') > -1 ) { let id = req.url.split('/'); let user = users.find( u => u['_id'] === Number(id[2]) ); @@ -23,15 +26,72 @@ function messageReceived(req, res) { let userJSON = JSON.stringify(user); res.write(userJSON); } + res.end() } - else { - res.write('Not Found') + // post a new user: + else if( req.method === 'POST' && req.url === '/users' ) { + postUsers(req, res); } - if( req.method === 'POST' && req.url === '/users' ) { - - } + // change a user: + else if( req.method === 'PUT' && req.url.indexOf('/users/') > -1 ) { + // putUsers(); + let body = []; + let id = req.url.split('/'); + let user = users.find( u => u['_id'] === Number(id[2]) ); - res.end(); + req.on( 'data', (chunk) => { + body.push(chunk); + }).on( 'end', () => { + body = Buffer.concat(body).toString(); + body = JSON.parse(body); + user.occupation = body.occupation; + res.write(JSON.stringify(user)) + res.end(); + }) + } + + // else { + // res.write('Not Found') + // } + + // res.end(); +} + +function postUsers(req, res) { + // initialize array to store the body: + let body = []; + req.on( 'data', (chunk) => { + body.push(chunk); + }).on( 'end', () => { + body = Buffer.concat(body).toString(); + // parse JSON to create an object(s) from the body: + let user = JSON.parse(body); + + // add _id property to new user: + user._id = users.length + 1; + + // push new user to users array in state.js: + users.push(user); + + // return the data we just updated to the client: + res.write(JSON.stringify(user)); + res.end(); + }) } + +// function putUsers(req, res) { +// let body = []; +// let id = req.url.split('/'); +// let user = users.find( u => u['_id'] === Number(id[2]) ); + +// req.on( 'data', (chunk) => { +// body.push(chunk); +// }).on( 'end', () => { +// body = Buffer.concat(body).toString(); +// body = JSON.parse(body); +// user.occupation = body.occupation; +// res.end(); +// }) +// } \ No newline at end of file From a742f6ee5a695d4544d8eb27ef385ba0a6cb13f6 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Tue, 30 Apr 2019 19:07:30 -0500 Subject: [PATCH 3/4] kept working, added some products functionality --- index.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index ee2575d..cbe4394 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,12 @@ function messageReceived(req, res) { res.end() } + // get all products: + else if( req.method === 'GET' && req.url === '/products' ) { + res.write( JSON.stringify(products) ); + res.end(); + } + // get specific user by id: else if( req.method === 'GET' && req.url.indexOf('/users') > -1 ) { let id = req.url.split('/'); @@ -29,6 +35,17 @@ function messageReceived(req, res) { res.end() } + // get specific product by id: + 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]) ); + if(!product) res.write('Product not found') + else { + let productJSON = JSON.stringify(product); + res.write(productJSON); + } + } + // post a new user: else if( req.method === 'POST' && req.url === '/users' ) { postUsers(req, res); @@ -52,9 +69,9 @@ function messageReceived(req, res) { }) } - // else { - // res.write('Not Found') - // } + else { + res.write('Not Found') + } // res.end(); } From 2e4af7585b9ea0fd5eb09e975983229644e7b6b4 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Tue, 30 Apr 2019 20:29:02 -0500 Subject: [PATCH 4/4] added products handlers and delete handlers --- index.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index cbe4394..af4ee89 100644 --- a/index.js +++ b/index.js @@ -36,14 +36,15 @@ function messageReceived(req, res) { } // get specific product by id: - else if( req.method === 'GET' && req.url.indexOf('/products') > -1 ) { + 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 product = products.find( p => p['id'] === Number(id[2]) ); if(!product) res.write('Product not found') else { let productJSON = JSON.stringify(product); res.write(productJSON); } + res.end(); } // post a new user: @@ -51,6 +52,28 @@ function messageReceived(req, res) { postUsers(req, res); } + // post a new product: + else if( req.method === 'POST' && req.url === '/products' ) { + let body = []; + req.on( 'data', (chunk) => { + body.push(chunk); + }).on( 'end', () => { + body = Buffer.concat(body).toString(); + // parse JSON to create an object(s) from the body: + let product = JSON.parse(body); + + // add _id property to new product: + product.id = products.length + 1; + + // push new product to products array in state.js: + products.push(product); + + // return the data we just updated to the client: + res.write(JSON.stringify(product)); + res.end(); + }) + } + // change a user: else if( req.method === 'PUT' && req.url.indexOf('/users/') > -1 ) { // putUsers(); @@ -69,6 +92,50 @@ function messageReceived(req, res) { }) } + // change a product: + else if( req.method === 'PUT' && req.url.indexOf('/products/') > -1 ) { + let body = []; + let id = req.url.split('/'); + let product = products.find( u => u['id'] === Number(id[2]) ); + + req.on( 'data', (chunk) => { + body.push(chunk); + }).on( 'end', () => { + body = Buffer.concat(body).toString(); + body = JSON.parse(body); + product.description = body.description; + res.write(JSON.stringify(product)) + res.end(); + }) + } + + // delete a user: + else if( req.method === 'DELETE' && req.url.indexOf('/users/') > -1 ) { + let id = req.url.split('/'); + let user = users.find( u => u['_id'] === Number(id[2]) ); + if(!user) { + res.write('User Not Found'); + } + else { + user.isActive = false; + res.write(`Deleted user ${user._id}: ${user.name}`); + } + res.end() + } + + else if( req.method === 'DELETE' && req.url.indexOf('/products/') > -1 ) { + let id = req.url.split('/'); + let product = products.find( p => p['id'] === Number(id[2]) ); + if(!product) { + res.write('Product not found'); + } + else { + product.isActive = false; + res.write(`Deleted product #${product.id}: ${product.name}`); + } + res.end(); + } + else { res.write('Not Found') }