From 091a77515222e2510f66e30ccae174e62728ccf5 Mon Sep 17 00:00:00 2001 From: Jared Gebel Date: Sun, 17 Sep 2017 16:59:02 -0400 Subject: [PATCH 1/4] Complete basic server --- app.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 app.js diff --git a/app.js b/app.js new file mode 100644 index 0000000..1f8d83a --- /dev/null +++ b/app.js @@ -0,0 +1,17 @@ +const http = require("http"); +const fs = require("fs"); + +const host = "127.0.0.1"; +const port = 3000; + +var server = http.createServer((req, res) => { + res.statusCode = 200; + res.setHeader("Content-Type", "text/plain"); + res.end("Hello World\n"); +}); + +server.listen(port, host, function() { + console.log( + `The server is now running and listening at the address http://${host}:${port}` + ); +}); From 6ef7410324bd31a48824844b9f1b6fe9f3939ab7 Mon Sep 17 00:00:00 2001 From: Jared Gebel Date: Sun, 17 Sep 2017 17:08:16 -0400 Subject: [PATCH 2/4] Set up index.html --- app.js | 12 +++++++++--- public/index.html | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 public/index.html diff --git a/app.js b/app.js index 1f8d83a..5ed967d 100644 --- a/app.js +++ b/app.js @@ -5,9 +5,15 @@ const host = "127.0.0.1"; const port = 3000; var server = http.createServer((req, res) => { - res.statusCode = 200; - res.setHeader("Content-Type", "text/plain"); - res.end("Hello World\n"); + 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); + } + }); }); server.listen(port, host, function() { diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..a5e13e5 --- /dev/null +++ b/public/index.html @@ -0,0 +1,17 @@ + + + + + + + My First Node.js Server + + + + + + +

This is a heading in My First Node.js Server

+ + + From b5c178aab1c310697be17a6a1e721862bd05a596 Mon Sep 17 00:00:00 2001 From: Jared Gebel Date: Sun, 17 Sep 2017 19:44:22 -0400 Subject: [PATCH 3/4] Complete displaying request and response data --- app.js | 30 ++++++++++++++++++++++++++++++ public/index.html | 10 ++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index 5ed967d..29fef25 100644 --- a/app.js +++ b/app.js @@ -11,6 +11,25 @@ var server = http.createServer((req, res) => { res.end("404 Not Found"); } else { res.writeHead(200, { "Content-Type": "text/html" }); + + var reqObject = { + url: req.url, + method: req.method, + httpVersion: req.httpVersion, + headers: req.headers + }; + var resObject = { + statusMessage: res.statusMessage, + statusCode: res.statusCode, + _header: res._header + }; + + var reqString = JSON.stringify(reqObject, null, 2); + + var resString = JSON.stringify(resObject, null, 2); + + data = data.replace(/{ req }/i, reqString); + data = data.replace(/{ res }/i, resString); res.end(data); } }); @@ -21,3 +40,14 @@ server.listen(port, host, function() { `The server is now running and listening at the address http://${host}:${port}` ); }); + +/* +req.url +req.method +req.httpVersion +req.headers + +res.statusMessage +res.statusCode +res._header +*/ diff --git a/public/index.html b/public/index.html index a5e13e5..502413d 100644 --- a/public/index.html +++ b/public/index.html @@ -6,12 +6,14 @@ My First Node.js Server - - - -

This is a heading in My First Node.js Server

+

Request:

+
{{ req }}
+ +

Response:

+
{{ res }}
+ From 014056cdf84d18a545001c31ef99496ee27cae5c Mon Sep 17 00:00:00 2001 From: Jared Gebel Date: Sun, 17 Sep 2017 21:33:13 -0400 Subject: [PATCH 4/4] Complete form --- public/index.html | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/public/index.html b/public/index.html index 502413d..36f6c22 100644 --- a/public/index.html +++ b/public/index.html @@ -9,11 +9,23 @@

This is a heading in My First Node.js Server

Request:

-
{{ req }}
+
{{ req }}

Response:

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