From 07abafff7699a5cf25c297086a22f88e170e7b58 Mon Sep 17 00:00:00 2001 From: Michael Bryant Date: Wed, 1 May 2019 21:35:49 -0700 Subject: [PATCH 1/5] Replace concatenated strings with template strings JavaScript has template strings, which allow embedded newlines and variable interpolation; this means that much less string trickery needs to happen to build the HTML. I've also used this to minimize the number of calls to `response.write()`. --- app.js | 78 +++++++++++++++++++++++++++------------------------------- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/app.js b/app.js index ba56311..fb64c87 100644 --- a/app.js +++ b/app.js @@ -7,59 +7,53 @@ var friends = require("./friends.json"); // Once for all times const server = http.createServer((request, response) => { response.statusCode = 200; response.setHeader('Content-type', 'text/html'); - response.write( - ' \n' + - ' \n' + - ' \n' + - ' \n' + - ' \n' + - ' \n' + - ' \n' + - ' \n' + - ' Netcentric Computing Home Page \n' + - ' \n' + - ' \n' + - ' \n' + - ' \n' + - '
\n' + - '

Another Example of Node


\n' - - ); var currentDate = new Date(); response.write( - '

Current time is: ' + currentDate + '

' - ); - response.write( - ' \n' + - ' \n' + - ' \n' + - ' \n' + - ' \n' + - ' \n' + - ' \n' + - ' \n' + - ' \n' + ` + + + + + + + + Netcentric Computing Home Page + + + + +
+

Another Example of Node


+

Current time is: ${currentDate}

+
First NameLast NamePhone
+ + + + + + + + +` ); for (var key in friends) for (var f in friends[key]) response.write( - ' \n' + - ' \n' + - ' \n' + - ' \n' + - ' \n' + ` + + +` ); response.write( - ' \n' + - '
First NameLast NamePhone
' + friends[key][f]["firstName"] + '' + friends[key][f]["lastName"] + '' + friends[key][f]["phone"] + '
${friends[key][f]["firstName"]}${friends[key][f]["lastName"]} + ${friends[key][f]["phone"]} +
\n' - ); - response.write( - ' \n' + - ' \n' + ` + + + ` ); response.end(); - }); server.listen(port, hostname, () => { From 4d3553732dc5f93c0349502e486c304743af5d44 Mon Sep 17 00:00:00 2001 From: Michael Bryant Date: Wed, 1 May 2019 22:21:21 -0700 Subject: [PATCH 2/5] Modify assignment of hostname and port I've been assigned 3005 as a port, so set that as default. Default hostname to "localhost" for development reasons. Add `./config.json` to gitignore. --- .gitignore | 3 +++ app.js | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index ad46b30..fffbab1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Ignore configuration files +config.json + # Logs logs *.log diff --git a/app.js b/app.js index fb64c87..82973e7 100644 --- a/app.js +++ b/app.js @@ -1,6 +1,9 @@ const http = require('http'); -const hostname = 'localhost'; -const port = 3000; +const fs = require('fs'); +var config = {}; +if (fs.existsSync('./config.json')) config = require('./config.json'); +const hostname = config.hostname || 'localhost'; +const port = config.port || 3005; var friends = require("./friends.json"); // Once for all times From fef9968c88d60e3253894ca79e91e0084061b22c Mon Sep 17 00:00:00 2001 From: Michael Bryant Date: Wed, 1 May 2019 22:52:10 -0700 Subject: [PATCH 3/5] Add genders for friends and a new friend --- app.js | 2 ++ friends.json | 11 ++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index 82973e7..eb52514 100644 --- a/app.js +++ b/app.js @@ -34,6 +34,7 @@ const server = http.createServer((request, response) => { First Name Last Name Phone + Gender @@ -46,6 +47,7 @@ const server = http.createServer((request, response) => { ${friends[key][f]["firstName"]} ${friends[key][f]["lastName"]} ${friends[key][f]["phone"]} + ${friends[key][f]["gender"]} ` ); diff --git a/friends.json b/friends.json index 48921c9..dc76f84 100644 --- a/friends.json +++ b/friends.json @@ -1,7 +1,8 @@ {"friends": [ - {"firstName": "George", "lastName": "Kristoferson", "phone": "2062062066"}, - {"firstName": "Bill", "lastName": "Madison", "phone": "4254254255"}, - {"firstName": "Peter", "lastName": "Frampton", "phone": "2125554545"}, - {"firstName": "Anna", "lastName": "Calixtus", "phone": "7259865321"}, - {"firstName": "Marie", "lastName": "Curie", "phone": "6017744991"} + {"firstName": "George", "lastName": "Kristoferson", "phone": "2062062066", "gender": "male"}, + {"firstName": "Bill", "lastName": "Madison", "phone": "4254254255", "gender": "male"}, + {"firstName": "Peter", "lastName": "Frampton", "phone": "2125554545", "gender": "male"}, + {"firstName": "Anna", "lastName": "Calixtus", "phone": "7259865321", "gender": "female"}, + {"firstName": "Marie", "lastName": "Curie", "phone": "6017744991", "gender": "female"}, + {"firstName": "William", "lastName": "Churchil", "phone": "2021122333", "gender": "male"} ]} From afef06fbdb5a8d99a613b57872a7bc0460e73081 Mon Sep 17 00:00:00 2001 From: Michael Bryant Date: Wed, 1 May 2019 23:02:05 -0700 Subject: [PATCH 4/5] Extra credit: Add new friends JSON and server New JSON file has a different structure. Technically the original code this assignment was based on could handle the JSON fine, but these changes add the keys as titles and makes each array a separate table. --- app2.js | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ friends2.json | 12 +++++++++ 2 files changed, 82 insertions(+) create mode 100644 app2.js create mode 100644 friends2.json diff --git a/app2.js b/app2.js new file mode 100644 index 0000000..cef682a --- /dev/null +++ b/app2.js @@ -0,0 +1,70 @@ +const http = require('http'); +const fs = require('fs'); +var config = {}; +if (fs.existsSync('./config.json')) config = require('./config.json'); +const hostname = config.hostname || 'localhost'; +const port = config.port || 3005; + +var friends = require("./friends2.json"); // Once for all times + +const server = http.createServer((request, response) => { + response.statusCode = 200; + response.setHeader('Content-type', 'text/html'); + var currentDate = new Date(); + response.write( + ` + + + + + + + + Netcentric Computing Home Page + + + + +
+

Another Example of Node


+

Current time is: ${currentDate}

` + ); + for (var key in friends) { + response.write(` +

${key.toUpperCase()}

+ + + + + + + + + ` + ); + for (var f in friends[key]) { + response.write( + ` + + +` + ); + } + response.write( + ` +
First NameLast NamePhone
${friends[key][f]["firstName"]}${friends[key][f]["lastName"]} + ${friends[key][f]["phone"]} +
` + ); + } + + response.write( + ` + ` + ); + response.end(); +}); + +server.listen(port, hostname, () => { + console.log(`Server running at http://${hostname}:${port}/`); +}); diff --git a/friends2.json b/friends2.json new file mode 100644 index 0000000..e23c243 --- /dev/null +++ b/friends2.json @@ -0,0 +1,12 @@ +{ + "boys": [ + {"firstName": "George", "lastName": "Kristoferson", "phone": "2062062066"}, + {"firstName": "Bill", "lastName": "Madison", "phone": "4254254255"}, + {"firstName": "Peter", "lastName": "Frampton", "phone": "2125554545"}, + {"firstName": "William", "lastName": "Churchil", "phone": "2021122333"} + ], + "girls": [ + {"firstName": "Anna", "lastName": "Calixtus", "phone": "7259865321"}, + {"firstName": "Marie", "lastName": "Curie", "phone": "6017744991"} + ] +} From 493776311be6f2f00fac004014d8b4485c3018bf Mon Sep 17 00:00:00 2001 From: Michael Bryant Date: Wed, 1 May 2019 23:11:18 -0700 Subject: [PATCH 5/5] Personalize the pages a bit --- app.js | 7 ++++--- app2.js | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app.js b/app.js index eb52514..d526881 100644 --- a/app.js +++ b/app.js @@ -19,14 +19,15 @@ const server = http.createServer((request, response) => { - - Netcentric Computing Home Page + + I Told You They're Not Imaginary!
-

Another Example of Node


+

A List of My Friends

+

Current time is: ${currentDate}

diff --git a/app2.js b/app2.js index cef682a..2123e15 100644 --- a/app2.js +++ b/app2.js @@ -19,14 +19,15 @@ const server = http.createServer((request, response) => { - - Netcentric Computing Home Page + + I Told You They're Not Imaginary!
-

Another Example of Node


+

A List of My Friends

+

Current time is: ${currentDate}

` ); for (var key in friends) {