From d2fbcf055414d9beb9c3fb5f100298b6f40c09dd Mon Sep 17 00:00:00 2001 From: Aaron Saloff Date: Sat, 2 Sep 2017 20:29:24 -0400 Subject: [PATCH 1/3] Setting Up the HTTP Server - completed --- 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..0e5174a --- /dev/null +++ b/app.js @@ -0,0 +1,17 @@ +var http = require('http'); +var fs = require('fs'); + +var port = 3000; +var host = 'localhost'; + +var server = http.createServer(function(req, res) { + res.writeHead(200, { + "Content-Type": 'text/html' + }); + res.end("

Hello World!

"); +}); + +server.listen(port, host, function() { + console.log(`Listening at http://${ host }:${ port }`); +}); + From bec758f7601c58a809948fabd2012e8902e8ee4d Mon Sep 17 00:00:00 2001 From: Aaron Saloff Date: Sat, 2 Sep 2017 20:34:56 -0400 Subject: [PATCH 2/3] Outputting HTML - completed --- app.js | 8 +++++--- public/index.html | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 public/index.html diff --git a/app.js b/app.js index 0e5174a..a05a8de 100644 --- a/app.js +++ b/app.js @@ -5,10 +5,12 @@ var port = 3000; var host = 'localhost'; var server = http.createServer(function(req, res) { - res.writeHead(200, { - "Content-Type": 'text/html' + fs.readFile('./public/index.html', 'utf8', function(err, data) { + 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..779c0bc --- /dev/null +++ b/public/index.html @@ -0,0 +1,16 @@ + + + + + + + Setting up Node Server + + + + + + +

Hello From Node.js!

+ + From 7135f3c93287ce69b38f9da59655145093bad198 Mon Sep 17 00:00:00 2001 From: Aaron Saloff Date: Sat, 2 Sep 2017 23:15:59 -0400 Subject: [PATCH 3/3] Displaying the Request and Response Data - completed --- app.js | 30 ++++++++++++++++++++++++++---- public/index.html | 6 +++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index a05a8de..ecdde9f 100644 --- a/app.js +++ b/app.js @@ -6,10 +6,32 @@ var host = 'localhost'; var server = http.createServer(function(req, res) { fs.readFile('./public/index.html', 'utf8', function(err, data) { - res.writeHead(200, { - "Content-Type": 'text/html' - }); - res.end(data); + if (err) { + res.writeHead(404); + res.end('There was an error'); + } else { + res.writeHead(200, { + "Content-Type": 'text/html' + }); + + var smallReq = { + url: req.url, + method: req.method, + httpVersion: req.httpVersion, + headers: req.headers + }; + + var smallRes = { + statusMessage: res.statusMessage, + statusCode: res.statusCode, + _header: res._header + }; + + var replaced = data.replace('{{ req }}', JSON.stringify(smallReq, null, 2)) + .replace('{{ res }}', JSON.stringify(smallRes, null, 2)); + + res.end(replaced); + } }); }); diff --git a/public/index.html b/public/index.html index 779c0bc..f5391c7 100644 --- a/public/index.html +++ b/public/index.html @@ -11,6 +11,10 @@ -

Hello From Node.js!

+

Request:

+
{{ req }}
+ +

Response:

+
{{ res }}