From 7deb184e26af61f6c8e4b15c9c12aa26ebb3f18f Mon Sep 17 00:00:00 2001 From: Madhavi Rajavasireddy Date: Tue, 5 Sep 2017 21:14:49 -0700 Subject: [PATCH 1/7] added name --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 29b1d71..57200bf 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # assignment_build_a_nodejs_server Building your first Node.js server and exploring the request and response objects + + +

Maddie Rajavasireddy

From 60effebc429bb2ccc52174212b1cb0f5645abb83 Mon Sep 17 00:00:00 2001 From: maddiereddy Date: Wed, 6 Sep 2017 09:50:56 -0700 Subject: [PATCH 2/7] completed setting up http server --- .gitignore | 1 + app.js | 17 +++++++++++++++++ package.json | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 .gitignore create mode 100644 app.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..7aa92a9 --- /dev/null +++ b/app.js @@ -0,0 +1,17 @@ +'use strict'; + +let http = require("http"); + +let host = "localhost"; +let port = 3000; + +let server = http.createServer(function(req, res) { + res.writeHead(200, { + "Content-Type": "text/plain" + }); + res.end("Hello World!"); +}); + +server.listen(port, host, function() { + console.log(`Listening at http://${host}:${port}`); +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..3e60f4c --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "assignment_build_a_nodejs_server", + "version": "1.0.0", + "description": "Building your first Node.js server and exploring the request and response objects", + "main": "app.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/maddiereddy/assignment_build_a_nodejs_server.git" + }, + "author": "Maddie Rajavasireddy", + "license": "ISC", + "bugs": { + "url": "https://github.com/maddiereddy/assignment_build_a_nodejs_server/issues" + }, + "homepage": "https://github.com/maddiereddy/assignment_build_a_nodejs_server#readme" +} From f36e051fae3df981ad368a58e044e8ce0fa81858 Mon Sep 17 00:00:00 2001 From: maddiereddy Date: Wed, 6 Sep 2017 10:04:01 -0700 Subject: [PATCH 3/7] completed outputting html --- app.js | 17 ++++++++++++++--- public/index.html | 11 +++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 public/index.html diff --git a/app.js b/app.js index 7aa92a9..5d8e081 100644 --- a/app.js +++ b/app.js @@ -1,15 +1,26 @@ 'use strict'; let http = require("http"); +let fs = require("fs"); let host = "localhost"; let port = 3000; let server = http.createServer(function(req, res) { - res.writeHead(200, { - "Content-Type": "text/plain" + // res.writeHead(200, { + // "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); + } }); - res.end("Hello World!"); }); server.listen(port, host, function() { diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..2925893 --- /dev/null +++ b/public/index.html @@ -0,0 +1,11 @@ + + + + + + NodeJS Server Test + + +

Hello NodeJS Server!

+ + \ No newline at end of file From c11de832371bf1363e016d49ab75d9c5a6b8c9f5 Mon Sep 17 00:00:00 2001 From: maddiereddy Date: Wed, 6 Sep 2017 10:20:32 -0700 Subject: [PATCH 4/7] completed displaying request and response data --- app.js | 24 ++++++++++++++++++++++++ public/index.html | 6 +++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 5d8e081..0c0c86c 100644 --- a/app.js +++ b/app.js @@ -18,6 +18,30 @@ let server = http.createServer(function(req, res) { res.end("404 Not Found"); } else { res.writeHead(200, {"Content-Type": "text/html"}); + + // capture incoming request data in an object + var reqObj = { + "req.url": req.url, + "req.method": req.method, + "req.httpVersion": req.httpVersion, + "req.headers": req.headers + } + + // capture response data in an object + var resObj = { + "res.statusCode": res.statusCode, + "res.statusMessage": res.statusMessage, + "res._header": res._header + } + + // output JSON objects info into strings + var reqOutput = JSON.stringify( reqObj, null, 2 ); + var resOutput = JSON.stringify( resObj, null, 2 ); + + // reassign values of html variables to new data string content + data = data.replace( "req", reqOutput ); + data = data.replace( "res", resOutput ); + res.end(data); } }); diff --git a/public/index.html b/public/index.html index 2925893..a08658c 100644 --- a/public/index.html +++ b/public/index.html @@ -6,6 +6,10 @@ NodeJS Server Test -

Hello NodeJS Server!

+

Hello NodeJS Server!

+

Request:

+
{{ req }}
+

Response:

+
{{ res }}
\ No newline at end of file From c92a52016f428b1ed0e5966cf934cd3cd237f86d Mon Sep 17 00:00:00 2001 From: maddiereddy Date: Wed, 6 Sep 2017 10:27:16 -0700 Subject: [PATCH 5/7] completed submitting a form --- app.js | 2 +- public/index.html | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 0c0c86c..b28051b 100644 --- a/app.js +++ b/app.js @@ -15,7 +15,7 @@ let server = http.createServer(function(req, res) { fs.readFile("./public/index.html", "utf8", function(err, data){ if (err) { res.writeHead(404); - res.end("404 Not Found"); + res.end("404: Page Not Found"); } else { res.writeHead(200, {"Content-Type": "text/html"}); diff --git a/public/index.html b/public/index.html index a08658c..b23e4a0 100644 --- a/public/index.html +++ b/public/index.html @@ -2,14 +2,31 @@ + + NodeJS Server Test + + + +

Hello NodeJS Server!

+

Request:

{{ req }}
+

Response:

{{ res }}
+ +
+

Simple Form

+
+ + + +
+
\ No newline at end of file From 6097436db10f35899f784dc457193570e6cc910a Mon Sep 17 00:00:00 2001 From: maddiereddy Date: Wed, 6 Sep 2017 11:42:15 -0700 Subject: [PATCH 6/7] completed optional post data --- app.js | 43 ++++++++++++++++++++++++++++++++----------- public/index.html | 21 ++++++++++++++++----- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/app.js b/app.js index b28051b..d8810f6 100644 --- a/app.js +++ b/app.js @@ -2,6 +2,7 @@ let http = require("http"); let fs = require("fs"); +let qs = require("querystring"); let host = "localhost"; let port = 3000; @@ -19,33 +20,53 @@ let server = http.createServer(function(req, res) { } else { res.writeHead(200, {"Content-Type": "text/html"}); - // capture incoming request data in an object - var reqObj = { + // get incoming request data in an object + let reqObj = { "req.url": req.url, "req.method": req.method, "req.httpVersion": req.httpVersion, "req.headers": req.headers } - // capture response data in an object - var resObj = { + // get response data in an object + let resObj = { "res.statusCode": res.statusCode, "res.statusMessage": res.statusMessage, "res._header": res._header } // output JSON objects info into strings - var reqOutput = JSON.stringify( reqObj, null, 2 ); - var resOutput = JSON.stringify( resObj, null, 2 ); + let reqOutput = JSON.stringify(reqObj, null, 2); + let resOutput = JSON.stringify(resObj, null, 2); // reassign values of html variables to new data string content - data = data.replace( "req", reqOutput ); - data = data.replace( "res", resOutput ); + data = data.replace("req", reqOutput); + data = data.replace("res", resOutput); - res.end(data); - } + if (req.method === "POST") { + + // get posted data + let postObj = ""; + req.on("data", function(data) { + postObj += data; + }); + + // when no more data + req.on("end", function() { + + // parse posted data + postObj = qs.parse(postObj); + let postOutput = JSON.stringify(postObj, null, 2); + + console.log(`POST Data: ${postOutput}`); + }); + } + } + + res.end(data); }); -}); + +}); server.listen(port, host, function() { console.log(`Listening at http://${host}:${port}`); diff --git a/public/index.html b/public/index.html index b23e4a0..55e9517 100644 --- a/public/index.html +++ b/public/index.html @@ -21,12 +21,23 @@

Response:

{{ res }}
-

Simple Form

-
- - - +

Simple Login Form - GET

+ + + +
+ +
+

Simple Login Form - POST

+
+ + + +
+
+
+
\ No newline at end of file From e7ac482b4336c7d1d7a12beb6960b94749633e41 Mon Sep 17 00:00:00 2001 From: Madhavi Rajavasireddy Date: Wed, 6 Sep 2017 12:16:02 -0700 Subject: [PATCH 7/7] Added description of assignment --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 57200bf..7d168b6 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,9 @@ Building your first Node.js server and exploring the request and response object

Maddie Rajavasireddy

+ +Run command `node app.js` to run assignment and view results at http://localhost:3000 + +Optional part of assignment: Submitting and Processing Post Data + +Attempted by printing posting data to console - works