From 0e45979062317a4be9ba50f83057c6d0bd5efbac Mon Sep 17 00:00:00 2001 From: DC Date: Wed, 20 Sep 2017 10:16:09 -0700 Subject: [PATCH 1/5] initiated --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 29b1d71..cec1be0 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 + +by Dennis C From 80636ae5878b3d8f238b455e588480f62623f2d3 Mon Sep 17 00:00:00 2001 From: DC Date: Wed, 20 Sep 2017 11:32:27 -0700 Subject: [PATCH 2/5] completed setting up http server --- app.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 app.js diff --git a/app.js b/app.js new file mode 100644 index 0000000..f187abf --- /dev/null +++ b/app.js @@ -0,0 +1,20 @@ +// +// Setup HTTP server +// + +'use strict'; + +const http = require('http'); + +const hostname = 'localhost'; +const port = 3001; + +const server = http.createServer(function(req,res) { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/html'); + res.end('Hello World!\n'); +}); + +server.listen(port, hostname, function () { + console.log(`Listening on http://${hostname}:${port}/`) +}); From b0b80c62241f8c35d0d0b44e59e97d85af877ab8 Mon Sep 17 00:00:00 2001 From: DC Date: Wed, 20 Sep 2017 14:13:58 -0700 Subject: [PATCH 3/5] completed 'outputting html' section --- app.js | 14 ++++++++++++++ public/index.html | 11 +++++++++++ 2 files changed, 25 insertions(+) create mode 100644 public/index.html diff --git a/app.js b/app.js index f187abf..f053d89 100644 --- a/app.js +++ b/app.js @@ -5,14 +5,28 @@ 'use strict'; const http = require('http'); +const fs = require('fs'); const hostname = 'localhost'; const port = 3001; const server = http.createServer(function(req,res) { + fs.readFile(__dirname + '/public/index.html', 'utf8', function(err,data) { + if (err) { + res.writeHead(404); + res.end("File not found"); + } else { + res.writeHead(200, + {'Content-Type': 'text/html'}); + res.end(data); + }; + }); + +/* res.statusCode = 200; res.setHeader('Content-Type', 'text/html'); res.end('Hello World!\n'); +*/ }); server.listen(port, hostname, function () { diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..286a881 --- /dev/null +++ b/public/index.html @@ -0,0 +1,11 @@ + + + + + + +

This page is a response from the server!

+ + + + From 3b7506f57441b4707babf451c932fe973dc4cef2 Mon Sep 17 00:00:00 2001 From: DC Date: Wed, 20 Sep 2017 15:48:22 -0700 Subject: [PATCH 4/5] completed json output --- app1-setup.js | 21 ++++++++++++++ app.js => app2-html.js | 8 +---- app3-json.js | 66 ++++++++++++++++++++++++++++++++++++++++++ public/index.html | 8 ++++- 4 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 app1-setup.js rename app.js => app2-html.js (80%) create mode 100644 app3-json.js diff --git a/app1-setup.js b/app1-setup.js new file mode 100644 index 0000000..75f4ec9 --- /dev/null +++ b/app1-setup.js @@ -0,0 +1,21 @@ +// +// 1) Setting up the HTTP server +// + +'use strict'; + +const http = require('http'); +const fs = require('fs'); + +const hostname = 'localhost'; +const port = 3001; + +const server = http.createServer(function(req,res) { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/html'); + res.end('Hello World!\n'); +}); + +server.listen(port, hostname, function () { + console.log(`Listening on http://${hostname}:${port}/`) +}); diff --git a/app.js b/app2-html.js similarity index 80% rename from app.js rename to app2-html.js index f053d89..b70762c 100644 --- a/app.js +++ b/app2-html.js @@ -1,5 +1,5 @@ // -// Setup HTTP server +// 2) Outputting HTML // 'use strict'; @@ -21,12 +21,6 @@ const server = http.createServer(function(req,res) { res.end(data); }; }); - -/* - res.statusCode = 200; - res.setHeader('Content-Type', 'text/html'); - res.end('Hello World!\n'); -*/ }); server.listen(port, hostname, function () { diff --git a/app3-json.js b/app3-json.js new file mode 100644 index 0000000..434bc7d --- /dev/null +++ b/app3-json.js @@ -0,0 +1,66 @@ +// +// 3) Displaying request and response data with JSON +// + +'use strict'; + +const http = require('http'); +const fs = require('fs'); + +const hostname = 'localhost'; +const port = 3001; + +const server = http.createServer(function(req,res) { + var reqSnip = { + url: req.url, + method: req.method, + httpVersion: req.httpVersion, + headers: req.headers + }; + + var resSnip = { + statusMessage: res.statusMessage, + statusCode: res.statusCode, + header: res._header + }; + + /* + //this won't work because objects are circular structure, ie, too big + //thus need to "downsize" it + var myJsonReqSnip = JSON.stringify(req, null, 2); + var myJsonResSnip = JSON.stringify(res, null, 2); + */ + + /* + //looks minified + var myJsonReqSnip = JSON.stringify(reqSnip); + var myJsonResSnip = JSON.stringify(resSnip); + */ + + //thus pretty print it + var myJsonReqSnip = JSON.stringify(reqSnip, null, 2); + var myJsonResSnip = JSON.stringify(resSnip, null, 2); + +/* + res.writeHead(200, + //using mime-type = application/json seems to produce errors + {'Content-Type': 'application/json'}); + res.end(myJsonReqSnip + '\n\n' + myJsonResSnip); +*/ + + fs.readFile(__dirname + '/public/index.html', 'utf8', function(err,data) { + if (err) { + res.writeHead(404); + res.end("File not found"); + } else { + res.writeHead(200, + {'Content-Type': 'text/html'}); + data = data.replace('{ req }', myJsonReqSnip).replace('{ res }', myJsonResSnip); + res.end(data); + }; + }); +}); + +server.listen(port, hostname, function () { + console.log(`Listening on http://${hostname}:${port}/`) +}); diff --git a/public/index.html b/public/index.html index 286a881..fff0107 100644 --- a/public/index.html +++ b/public/index.html @@ -4,7 +4,13 @@ -

This page is a response from the server!

+

This page is a response from the server!

+ +

Request:

+
{ req }
+ +

Response:

+
{ res }
From 713ba8f0c5d7adf314c98a15d76d479f4b7649dc Mon Sep 17 00:00:00 2001 From: DC Date: Wed, 20 Sep 2017 16:00:41 -0700 Subject: [PATCH 5/5] completed! --- public/index.html | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/public/index.html b/public/index.html index fff0107..6b8521a 100644 --- a/public/index.html +++ b/public/index.html @@ -11,6 +11,20 @@

Request:

Response:

{ res }
+ +
+
+ Submitting a form + + + + + + + + +
+