From 8cb03222e67b812ae1e4834ffa47cadc47f1321b Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Mon, 30 Oct 2017 14:35:49 -0500 Subject: [PATCH 1/4] initial commit, http server set up --- app.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 app.js diff --git a/app.js b/app.js new file mode 100644 index 0000000..53a7d3c --- /dev/null +++ b/app.js @@ -0,0 +1,18 @@ +var http = require('http'); + + +var port = 3000; +var host = 'localhost'; + + +var server = http.createServer(function (req, res) { + res.setHeader('Content-Type', 'text/plain'); + res.end("Hello World!") + +}); + + + +server.listen(port, host, function () { + console.log(`Server: Live || Listening: ${host}:${port}`) +}); From 45966854dd40a923b17d7ba55cd8b34bd3315bb9 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Mon, 30 Oct 2017 14:44:48 -0500 Subject: [PATCH 2/4] outputting html --- app.js | 15 ++++++++++++--- public/index.html | 9 +++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 public/index.html diff --git a/app.js b/app.js index 53a7d3c..91df6b1 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,5 @@ var http = require('http'); +var fs = require('fs'); var port = 3000; @@ -6,9 +7,17 @@ var host = 'localhost'; var server = http.createServer(function (req, res) { - res.setHeader('Content-Type', 'text/plain'); - res.end("Hello World!") - + 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); + } + }) }); diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..07372b9 --- /dev/null +++ b/public/index.html @@ -0,0 +1,9 @@ + + + + +

Hello (node) World!

+ + + + From 5a04d3dd27a677e8b23d305d444d845c251739c4 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Mon, 30 Oct 2017 17:35:17 -0500 Subject: [PATCH 3/4] request & response data displaying --- app.js | 19 +++++++++++++------ public/index.html | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/app.js b/app.js index 91df6b1..03e5da0 100644 --- a/app.js +++ b/app.js @@ -8,14 +8,21 @@ var host = 'localhost'; var server = http.createServer(function (req, res) { fs.readFile('./public/index.html', 'utf8', function (err,data) { + request_obj = {URL: req.url, METHOD: req.method, + 'HTTP VERSION':req.httpVersion, HEADERS: req.headers}; + result_obj = {STATUS:res.statusMessage, CODE:res.statusCode, + HEADER:res._header} + if (err) { - res.writeHead(404); - res.end("404 Not Found"); + res.writeHead(404); + res.end("404 Not Found"); } else { - res.writeHead(200, { - "Content-Type" : "text/html" - }); - res.end(data); + res.writeHead(200, { + "Content-Type" : "text/html" + }); + data = data.replace('{{ req }}', JSON.stringify(request_obj, null, 5)); + data = data.replace('{{ res }}', JSON.stringify(result_obj, null, 5)); + res.end(data); } }) }); diff --git a/public/index.html b/public/index.html index 07372b9..f186c9b 100644 --- a/public/index.html +++ b/public/index.html @@ -1,9 +1,19 @@ + + Node Server + +

Hello (node) World!

- - +

Request:

+
{{ req }}
+ +

Response:

+
{{ res }}
+ + + From 7e8b6f0b3585e608bf61878dc420a9022e318754 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Mon, 30 Oct 2017 19:39:13 -0500 Subject: [PATCH 4/4] assignment compeleted --- public/index.html | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/public/index.html b/public/index.html index f186c9b..3534e1c 100644 --- a/public/index.html +++ b/public/index.html @@ -16,4 +16,13 @@

Response:

{{ res }}
+
+
+ +
+ + +
+