From 78e8deaa5aa0bd933328c132931bc1b1cbd4e925 Mon Sep 17 00:00:00 2001 From: TopDogIRE Date: Tue, 19 Jan 2021 22:40:23 -0800 Subject: [PATCH 1/3] initial Commit to week2 --- httpServerNode.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 httpServerNode.js diff --git a/httpServerNode.js b/httpServerNode.js new file mode 100644 index 0000000..e50c975 --- /dev/null +++ b/httpServerNode.js @@ -0,0 +1,42 @@ +const http = require('http'); +const users = [ + {user:'Kris', age:67}, + {user:'Tommy', age:32}, + {user:'Alan', age:23}, + {user:'Peg', age:24}, + {user:'Rebecca', age:20}, + {user:'Indika', age:27}, +]; + +const requestHandler = function(req, res){ + + if (req.method === 'GET' && req.url == '/users') { + res.writeHead(200); + res.write(`${JSON.stringify(users)}`); + res.end(); + + }else if (req.method === 'POST' && req.url == '/') { + res.writeHead(200); + let chunks = ""; + + req.on('data', (chunk) => { + chunks += chunk; + }); + + req.on('end', () => { + const jsonData = JSON.parse(chunks); + console.log(JSON.stringify(jsonData)); + res.write(`

${jsonData.toString()}

`); + res.end(); + }); + + }else{ + res.end('Welecome to the page'); + } + +} + +const server = http.createServer(requestHandler); +server.listen(8080); + +// http://localhost:8080/users \ No newline at end of file From b0ff6186d3ff895548a0b02286e07ebb54a036f4 Mon Sep 17 00:00:00 2001 From: TopDogIRE <60242692+TopDogIRE@users.noreply.github.com> Date: Sun, 24 Jan 2021 22:04:52 -0800 Subject: [PATCH 2/3] update on comments and post to DOM --- httpServerNode.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/httpServerNode.js b/httpServerNode.js index e50c975..774e820 100644 --- a/httpServerNode.js +++ b/httpServerNode.js @@ -1,5 +1,5 @@ const http = require('http'); -const users = [ +const users = [ // Users info {user:'Kris', age:67}, {user:'Tommy', age:32}, {user:'Alan', age:23}, @@ -10,14 +10,14 @@ const users = [ const requestHandler = function(req, res){ - if (req.method === 'GET' && req.url == '/users') { + if (req.method === 'GET' && req.url == '/users') { // Handles Get from /users res.writeHead(200); res.write(`${JSON.stringify(users)}`); res.end(); - }else if (req.method === 'POST' && req.url == '/') { + }else if (req.method === 'POST' && req.url == '/') { // Handles Post res.writeHead(200); - let chunks = ""; + let chunks = ""; // Initialize String req.on('data', (chunk) => { chunks += chunk; @@ -25,18 +25,16 @@ const requestHandler = function(req, res){ req.on('end', () => { const jsonData = JSON.parse(chunks); - console.log(JSON.stringify(jsonData)); - res.write(`

${jsonData.toString()}

`); + const namesOfJson = jsonData.user; // Getting the name form the array of objects + res.write(`

${nameOfJson}

`); res.end(); }); }else{ - res.end('Welecome to the page'); + res.end('Welecome to the page'); // Welcoming on the home page } } const server = http.createServer(requestHandler); -server.listen(8080); - -// http://localhost:8080/users \ No newline at end of file +server.listen(8080); // http://localhost:8080/users From ba4833a1fd04376f1c1732a677983d8b56651019 Mon Sep 17 00:00:00 2001 From: TopDogIRE <60242692+TopDogIRE@users.noreply.github.com> Date: Tue, 26 Jan 2021 14:55:05 -0800 Subject: [PATCH 3/3] Removing whitespace --- httpServerNode.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/httpServerNode.js b/httpServerNode.js index 774e820..b53f910 100644 --- a/httpServerNode.js +++ b/httpServerNode.js @@ -7,34 +7,27 @@ const users = [ // Users info {user:'Rebecca', age:20}, {user:'Indika', age:27}, ]; - +// Deal with request from page const requestHandler = function(req, res){ - if (req.method === 'GET' && req.url == '/users') { // Handles Get from /users res.writeHead(200); res.write(`${JSON.stringify(users)}`); res.end(); - }else if (req.method === 'POST' && req.url == '/') { // Handles Post res.writeHead(200); let chunks = ""; // Initialize String - req.on('data', (chunk) => { chunks += chunk; }); - req.on('end', () => { const jsonData = JSON.parse(chunks); const namesOfJson = jsonData.user; // Getting the name form the array of objects res.write(`

${nameOfJson}

`); res.end(); }); - }else{ res.end('Welecome to the page'); // Welcoming on the home page } - } - const server = http.createServer(requestHandler); server.listen(8080); // http://localhost:8080/users