From 40be1332fc703ffda668604d571a4e25a654a65c Mon Sep 17 00:00:00 2001 From: Steven Zarrella Date: Wed, 27 Sep 2017 16:03:38 -0400 Subject: [PATCH 1/4] HTTP server set up --- README.md | 2 ++ app.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 app.js diff --git a/README.md b/README.md index 29b1d71..2072e28 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # assignment_build_a_nodejs_server Building your first Node.js server and exploring the request and response objects + +Steven Zarrella \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..e150ec4 --- /dev/null +++ b/app.js @@ -0,0 +1,15 @@ +"use strict"; + +const http = require('http'); + +let server = http.createServer((req, res) => { + + res.writeHead(200, { + "Content-Type": "text/html" + }); + res.end("

Hello world!

"); +}); + +server.listen(3000, 'localhost', function() { + console.log("Listening at http://localhost:3000"); +}); \ No newline at end of file From 9ea909b9528368b1a47867061f54d6759dc9d42b Mon Sep 17 00:00:00 2001 From: Steven Zarrella Date: Wed, 27 Sep 2017 16:12:46 -0400 Subject: [PATCH 2/4] outputting HTML section complete --- app.js | 17 ++++++++++++----- public/index.html | 10 ++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 public/index.html diff --git a/app.js b/app.js index e150ec4..67c7992 100644 --- a/app.js +++ b/app.js @@ -1,13 +1,20 @@ "use strict"; -const http = require('http'); +const http = require('http'), + fs = require('fs'); let server = http.createServer((req, res) => { - - res.writeHead(200, { - "Content-Type": "text/html" + fs.readFile('./public/index.html', 'utf8', function(err, data) { + if (err) { + res.writeHead(404); + res.end("404 - Not Found"); + } else { + res.writeHead(200, { + "Content-Type": "text/html" + }); + res.end(data); + } }); - res.end("

Hello world!

"); }); server.listen(3000, 'localhost', function() { diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..0d55bea --- /dev/null +++ b/public/index.html @@ -0,0 +1,10 @@ + + + + + Build a Node.js server + + +

Hello World!

+ + \ No newline at end of file From 66e378feca03943b90d6d05877d8eb7422e7de25 Mon Sep 17 00:00:00 2001 From: Steven Zarrella Date: Thu, 28 Sep 2017 09:35:31 -0400 Subject: [PATCH 3/4] displaying req/res data complete --- app.js | 25 ++++++++++++++++++++++++- public/index.html | 8 +++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 67c7992..5530133 100644 --- a/app.js +++ b/app.js @@ -3,6 +3,12 @@ const http = require('http'), fs = require('fs'); +let JSONTestData = { + "poop": "woof", + "meow": "boo", + "ribbit": "ham sandwich" +}; + let server = http.createServer((req, res) => { fs.readFile('./public/index.html', 'utf8', function(err, data) { if (err) { @@ -12,7 +18,24 @@ let server = http.createServer((req, res) => { res.writeHead(200, { "Content-Type": "text/html" }); - res.end(data); + + let reqArray = ["url", "method", "httpVersion", "headers"], + reqObjectArray = [], + resArray = ["statusMessage", "statusCode", "_header"], + resObjectArray = []; + + for (var item in reqArray) { + reqObjectArray.push({[reqArray[item]]: req[reqArray[item]]}); + }; + + for (var item in resArray) { + resObjectArray.push({[resArray[item]]: res[resArray[item]]}); + }; + + let newData = data.replace("{{ req }}", JSON.stringify(reqObjectArray)); + newData = newData.replace("{{ res }}", JSON.stringify(resObjectArray)); + + res.end(newData); } }); }); diff --git a/public/index.html b/public/index.html index 0d55bea..3bd4298 100644 --- a/public/index.html +++ b/public/index.html @@ -5,6 +5,12 @@ Build a Node.js server -

Hello World!

+

Meow World!


+

Request:

+
{{ req }}

+ +

Response:

+
{{ res }}
+ \ No newline at end of file From 6907b6ac7bf8ae80e9a2583fc4d5b18e92e1f3ea Mon Sep 17 00:00:00 2001 From: Steven Zarrella Date: Thu, 28 Sep 2017 10:39:58 -0400 Subject: [PATCH 4/4] project complete --- app.js | 8 +------- public/index.html | 6 ++++++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index 5530133..8540e72 100644 --- a/app.js +++ b/app.js @@ -3,12 +3,6 @@ const http = require('http'), fs = require('fs'); -let JSONTestData = { - "poop": "woof", - "meow": "boo", - "ribbit": "ham sandwich" -}; - let server = http.createServer((req, res) => { fs.readFile('./public/index.html', 'utf8', function(err, data) { if (err) { @@ -18,7 +12,7 @@ let server = http.createServer((req, res) => { res.writeHead(200, { "Content-Type": "text/html" }); - + let reqArray = ["url", "method", "httpVersion", "headers"], reqObjectArray = [], resArray = ["statusMessage", "statusCode", "_header"], diff --git a/public/index.html b/public/index.html index 3bd4298..6ea8b98 100644 --- a/public/index.html +++ b/public/index.html @@ -6,6 +6,12 @@

Meow World!


+
+

username:

+

password:

+ +
+

Request:

{{ req }}