From 41d677b2338da2a80bd3ae702b54612d9bd835e2 Mon Sep 17 00:00:00 2001 From: Jurgen n Date: Mon, 2 Aug 2021 11:18:12 +0200 Subject: [PATCH 1/4] Initial commit --- app.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 app.js diff --git a/app.js b/app.js new file mode 100644 index 0000000..47fd634 --- /dev/null +++ b/app.js @@ -0,0 +1,21 @@ +var http = require("http"); + +var server = http.createServer +( + (req, res) => + { + res.end("Hello World!"); + } +); + +var host = "localhost"; +var port = 3000; + +server.listen +( + port, host, () => + { + console.log(`Listening at http://${host}:${port}`); + } +); + From 183984d4513e1020e8fb51f6b19897991d9a9b34 Mon Sep 17 00:00:00 2001 From: Jurgen n Date: Mon, 2 Aug 2021 11:27:40 +0200 Subject: [PATCH 2/4] Output HTML --- app.js | 18 +++++++++++++++++- public/index.html | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 public/index.html diff --git a/app.js b/app.js index 47fd634..3ae7ba0 100644 --- a/app.js +++ b/app.js @@ -1,10 +1,26 @@ var http = require("http"); +var fs = require("fs"); + +var path = "./public/index.html"; var server = http.createServer ( (req, res) => { - res.end("Hello World!"); + fs.readFile + ( + path, (err, data) => + { + if (err) + { + res.end("404 file not found"); + } + else + { + res.end(data); + } + } + ) } ); diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..597ecf5 --- /dev/null +++ b/public/index.html @@ -0,0 +1 @@ +

Hello world!

From 87214a6136c22830b4db315f5bbba1e14ca54e58 Mon Sep 17 00:00:00 2001 From: Jurgen n Date: Fri, 6 Aug 2021 17:40:21 +0200 Subject: [PATCH 3/4] Display the Request and Response Data --- app.js | 53 +++++++++++++++++++++++------------------------ public/index.html | 6 +++++- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/app.js b/app.js index 3ae7ba0..f03ffc6 100644 --- a/app.js +++ b/app.js @@ -3,35 +3,34 @@ var fs = require("fs"); var path = "./public/index.html"; -var server = http.createServer -( - (req, res) => - { - fs.readFile - ( - path, (err, data) => - { - if (err) - { - res.end("404 file not found"); - } - else - { - res.end(data); - } - } - ) - } -); +var server = http.createServer((req, res) => { + fs.readFile(path, "utf8", (err, data) => { + if (err) { + res.end("404 file not found"); + } else { + res.setHeader("Content-type", "text/html"); + reqFiltered = { + "url": req.url, + "method": req.method, + "httpVersion": req.httpVersion, + "headers": req.headers + }; + resFiltered = { + "statusMessage": res.statusMessage, + "statusCode": res.statusCode, + "_header": res._header + }; + data = data.replace("{{ req }}", JSON.stringify(reqFiltered, null, 2)); + data = data.replace("{{ res }}", JSON.stringify(resFiltered, null, 2)); + res.end(data); + } + }); +}); var host = "localhost"; var port = 3000; -server.listen -( - port, host, () => - { - console.log(`Listening at http://${host}:${port}`); - } -); +server.listen(port, host, () => { + console.log(`Listening at http://${host}:${port}`); +}); diff --git a/public/index.html b/public/index.html index 597ecf5..2872891 100644 --- a/public/index.html +++ b/public/index.html @@ -1 +1,5 @@ -

Hello world!

+

Request:

+
{{ req }}
+ +

Response:

+
{{ res }}
From 0ffd63f97af1c291313c3644490dc61dbf78ff42 Mon Sep 17 00:00:00 2001 From: Jurgen n Date: Fri, 6 Aug 2021 18:07:42 +0200 Subject: [PATCH 4/4] Submit Form --- app.js | 12 ++++++++---- public/index.html | 10 ++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index f03ffc6..82891e1 100644 --- a/app.js +++ b/app.js @@ -9,19 +9,23 @@ var server = http.createServer((req, res) => { res.end("404 file not found"); } else { res.setHeader("Content-type", "text/html"); - reqFiltered = { + + request = { "url": req.url, "method": req.method, "httpVersion": req.httpVersion, "headers": req.headers }; - resFiltered = { + + response = { "statusMessage": res.statusMessage, "statusCode": res.statusCode, "_header": res._header }; - data = data.replace("{{ req }}", JSON.stringify(reqFiltered, null, 2)); - data = data.replace("{{ res }}", JSON.stringify(resFiltered, null, 2)); + + data = data.replace("{{ req }}", JSON.stringify(request, null, 2)); + data = data.replace("{{ res }}", JSON.stringify(response, null, 2)); + res.end(data); } }); diff --git a/public/index.html b/public/index.html index 2872891..cc14a5f 100644 --- a/public/index.html +++ b/public/index.html @@ -1,3 +1,13 @@ +
+ + + + + + + +
+

Request:

{{ req }}