From 21cef94965d4f0433881921047bea8f2112100f0 Mon Sep 17 00:00:00 2001 From: Thomas Hauge Date: Thu, 31 Aug 2017 10:40:48 +0200 Subject: [PATCH 1/4] server up and running --- app.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 app.js diff --git a/app.js b/app.js new file mode 100644 index 0000000..477d631 --- /dev/null +++ b/app.js @@ -0,0 +1,15 @@ +var http = require('http'); + +var port = 3000; +var host = 'localhost'; + +var server = http.createServer(function(req, res) { + res.writeHead(200, { + 'Content-Type': 'text/plain' + }); + res.end('Hello World!'); +}); + +server.listen(port, host, function() { + console.log(`Server is running and listening at the address http://${host}:${port}`); +}); From 55181267100d5612f9c36e4910252f45e24cfa4d Mon Sep 17 00:00:00 2001 From: Thomas Hauge Date: Thu, 31 Aug 2017 10:48:52 +0200 Subject: [PATCH 2/4] outputting HTML --- app.js | 14 +++++++++++--- public/index.html | 12 ++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 public/index.html diff --git a/app.js b/app.js index 477d631..3f1750e 100644 --- a/app.js +++ b/app.js @@ -1,13 +1,21 @@ 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/plain' + fs.readFile('./public/index.html', 'utf8', function(err, data) { + if (err) { + res.writeHead(404); + res.end('404 Page 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..f541bd6 --- /dev/null +++ b/public/index.html @@ -0,0 +1,12 @@ + + + + + + + Node.js Server Practice + + +

you should see this

+ + From c42523c95d2d9ba75f3a1fb94ed83075f8a45f80 Mon Sep 17 00:00:00 2001 From: Thomas Hauge Date: Thu, 31 Aug 2017 11:19:15 +0200 Subject: [PATCH 3/4] displaying request and response data --- app.js | 18 +++++++++++++++++- public/index.html | 7 +++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 3f1750e..5893cf3 100644 --- a/app.js +++ b/app.js @@ -13,7 +13,23 @@ var server = http.createServer(function(req, res) { res.writeHead(200, { 'Content-Type': 'text/html' }); - res.end(data); + var myReq = { + url: req.url, + method: req.method, + httpVersion: req.httpVersion, + headers: req.headers + }; + var myRes = { + statusMessage: res.statusMessage, + statusCode: res.statusCode, + _header: res._header + }; + myReq = JSON.stringify(myReq, null, 2); + myRes = JSON.stringify(myRes, null, 2); + + res.end(data + .replace('{{ req }}', myReq) + .replace('{{ res }}', myRes)); }; }); }); diff --git a/public/index.html b/public/index.html index f541bd6..1537e20 100644 --- a/public/index.html +++ b/public/index.html @@ -8,5 +8,12 @@

you should see this

+ +

Request:

+
{{ req }}
+ +

Response:

+
{{ res }}
+ From 1faa541b77b46f1b5db737399e8a6a3662b85db5 Mon Sep 17 00:00:00 2001 From: Thomas Hauge Date: Thu, 31 Aug 2017 11:34:30 +0200 Subject: [PATCH 4/4] submitting form --- public/index.html | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/public/index.html b/public/index.html index 1537e20..0d06dac 100644 --- a/public/index.html +++ b/public/index.html @@ -15,5 +15,11 @@

Request:

Response:

{{ res }}
+
+ + + +
+