From 29757b9e591d97e960642a9aab729865c236c61e Mon Sep 17 00:00:00 2001 From: Megan Taylor Date: Sun, 10 Jun 2018 14:03:43 -0400 Subject: [PATCH 1/7] init --- .gitignore | 1 + index.js | 0 package.json | 19 +++++++++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/index.js b/index.js new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json new file mode 100644 index 0000000..1e02c7f --- /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": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/megantaylor/assignment_build_a_nodejs_server.git" + }, + "author": "megan taylor", + "license": "ISC", + "bugs": { + "url": "https://github.com/megantaylor/assignment_build_a_nodejs_server/issues" + }, + "homepage": "https://github.com/megantaylor/assignment_build_a_nodejs_server#readme" +} \ No newline at end of file From dd141628b2dd82d52a2b8902f2242b7eb8f7ce8f Mon Sep 17 00:00:00 2001 From: Megan Taylor Date: Sun, 10 Jun 2018 14:12:29 -0400 Subject: [PATCH 2/7] setup --- index.js | 8 ++++++ package-lock.json | 62 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 8 ++++-- 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 package-lock.json diff --git a/index.js b/index.js index e69de29..9e3682a 100644 --- a/index.js +++ b/index.js @@ -0,0 +1,8 @@ +var _ = require("lodash"); +var chalk = require('chalk'); + +console.log(chalk.blue('Hello world!')); + +_.each([1, 2], function(value) { + console.log(value); +}); diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..c6c3d8e --- /dev/null +++ b/package-lock.json @@ -0,0 +1,62 @@ +{ + "name": "assignment_build_a_nodejs_server", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "ansi-styles": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", + "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", + "requires": { + "color-convert": "^1.9.0" + } + }, + "chalk": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz", + "integrity": "sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ==", + "requires": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + } + }, + "color-convert": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.1.tgz", + "integrity": "sha512-mjGanIiwQJskCC18rPR6OmrZ6fm2Lc7PeGFYwCmy5J34wC6F1PzdGL6xeMfmgicfYcNLGuVFA3WzXtIDCQSZxQ==", + "requires": { + "color-name": "^1.1.1" + } + }, + "color-name": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" + }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" + }, + "has-flag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" + }, + "lodash": { + "version": "4.17.10", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz", + "integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==" + }, + "supports-color": { + "version": "5.4.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz", + "integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==", + "requires": { + "has-flag": "^3.0.0" + } + } + } +} diff --git a/package.json b/package.json index 1e02c7f..582142e 100644 --- a/package.json +++ b/package.json @@ -15,5 +15,9 @@ "bugs": { "url": "https://github.com/megantaylor/assignment_build_a_nodejs_server/issues" }, - "homepage": "https://github.com/megantaylor/assignment_build_a_nodejs_server#readme" -} \ No newline at end of file + "homepage": "https://github.com/megantaylor/assignment_build_a_nodejs_server#readme", + "dependencies": { + "chalk": "^2.4.1", + "lodash": "^4.17.10" + } +} From 3d676972603284868621337c8bc29ad1b363dfec Mon Sep 17 00:00:00 2001 From: Megan Taylor Date: Sun, 10 Jun 2018 14:22:44 -0400 Subject: [PATCH 3/7] oops, was totally looking at the wrong instructions --- app.js | 0 index.js | 8 ------ package-lock.json | 62 ----------------------------------------------- package.json | 5 ++-- 4 files changed, 2 insertions(+), 73 deletions(-) create mode 100644 app.js delete mode 100644 index.js delete mode 100644 package-lock.json diff --git a/app.js b/app.js new file mode 100644 index 0000000..e69de29 diff --git a/index.js b/index.js deleted file mode 100644 index 9e3682a..0000000 --- a/index.js +++ /dev/null @@ -1,8 +0,0 @@ -var _ = require("lodash"); -var chalk = require('chalk'); - -console.log(chalk.blue('Hello world!')); - -_.each([1, 2], function(value) { - console.log(value); -}); diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index c6c3d8e..0000000 --- a/package-lock.json +++ /dev/null @@ -1,62 +0,0 @@ -{ - "name": "assignment_build_a_nodejs_server", - "version": "1.0.0", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "ansi-styles": { - "version": "3.2.1", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", - "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", - "requires": { - "color-convert": "^1.9.0" - } - }, - "chalk": { - "version": "2.4.1", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz", - "integrity": "sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ==", - "requires": { - "ansi-styles": "^3.2.1", - "escape-string-regexp": "^1.0.5", - "supports-color": "^5.3.0" - } - }, - "color-convert": { - "version": "1.9.1", - "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.1.tgz", - "integrity": "sha512-mjGanIiwQJskCC18rPR6OmrZ6fm2Lc7PeGFYwCmy5J34wC6F1PzdGL6xeMfmgicfYcNLGuVFA3WzXtIDCQSZxQ==", - "requires": { - "color-name": "^1.1.1" - } - }, - "color-name": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", - "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" - }, - "escape-string-regexp": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", - "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" - }, - "has-flag": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", - "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" - }, - "lodash": { - "version": "4.17.10", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz", - "integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==" - }, - "supports-color": { - "version": "5.4.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz", - "integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==", - "requires": { - "has-flag": "^3.0.0" - } - } - } -} diff --git a/package.json b/package.json index 582142e..21c34a2 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,6 @@ }, "homepage": "https://github.com/megantaylor/assignment_build_a_nodejs_server#readme", "dependencies": { - "chalk": "^2.4.1", - "lodash": "^4.17.10" + } -} +} \ No newline at end of file From e109595754356b24c4af77284fbc7a29f0764149 Mon Sep 17 00:00:00 2001 From: Megan Taylor Date: Sun, 10 Jun 2018 14:25:10 -0400 Subject: [PATCH 4/7] hello world server --- app.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app.js b/app.js index e69de29..dd3799b 100644 --- a/app.js +++ b/app.js @@ -0,0 +1,14 @@ +const http = require('http'); + +const host = '127.0.0.1'; +const port = 3000; + +var server = http.createServer(function(req, res) { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/plain'); + res.end('Hello World!\n'); +}); + +server.listen(port, host, function() { + console.log(`Listening at http://${ host }:${ port }`); +}); From e701fdbe2ff4c2cb7072724b4c721c8aecc7cfa5 Mon Sep 17 00:00:00 2001 From: Megan Taylor Date: Sun, 10 Jun 2018 14:30:28 -0400 Subject: [PATCH 5/7] now with HTML --- app.js | 15 ++++++++++++--- public/index.html | 11 +++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 public/index.html diff --git a/app.js b/app.js index dd3799b..7fe0e11 100644 --- a/app.js +++ b/app.js @@ -1,12 +1,21 @@ const http = require('http'); +const fs = require('fs'); const host = '127.0.0.1'; const port = 3000; var server = http.createServer(function(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..a4d6508 --- /dev/null +++ b/public/index.html @@ -0,0 +1,11 @@ + + + + + +

My First Heading

+

My first paragraph.

+ + + + \ No newline at end of file From 51d56a856b89a699d3b14464effe5b2e9a9e2b02 Mon Sep 17 00:00:00 2001 From: Megan Taylor Date: Sun, 10 Jun 2018 14:37:18 -0400 Subject: [PATCH 6/7] using req, res in HTML --- app.js | 20 +++++++++++++++++++- public/index.html | 7 +++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app.js b/app.js index 7fe0e11..3ed7f5f 100644 --- a/app.js +++ b/app.js @@ -13,7 +13,25 @@ var server = http.createServer(function(req, res) { res.writeHead(200, { "Content-Type": "text/html" }); - res.end(data); + + let requested = { + url: req.url, + method: req.method, + httpVersion: req.httpVersion, + headers: req.headers + }; + + let responses = { + statusMessage: res.statusMessage, + statusCode: res.statusCode, + header: res._header + }; + + let modified = data + .replace(/{{ req }}/, JSON.stringify(requested, null, 1)) + .replace(/{{ res }}/, JSON.stringify(responses, null, 1)); + + res.end(modified); } }); }); diff --git a/public/index.html b/public/index.html index a4d6508..ab8d20d 100644 --- a/public/index.html +++ b/public/index.html @@ -3,8 +3,11 @@ -

My First Heading

-

My first paragraph.

+

Request:

+
{{ req }}
+ +

Response:

+
{{ res }}
From da6070bce2c82af1a7a67d4a16536449f48372f3 Mon Sep 17 00:00:00 2001 From: Megan Taylor Date: Sun, 10 Jun 2018 14:42:56 -0400 Subject: [PATCH 7/7] simple form submission --- public/index.html | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/public/index.html b/public/index.html index ab8d20d..1ec77a3 100644 --- a/public/index.html +++ b/public/index.html @@ -3,6 +3,12 @@ +
+ + + +
+

Request:

{{ req }}