diff --git a/README.md b/README.md index 29b1d71..cec1be0 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # assignment_build_a_nodejs_server Building your first Node.js server and exploring the request and response objects + +by Dennis C diff --git a/app1-setup.js b/app1-setup.js new file mode 100644 index 0000000..75f4ec9 --- /dev/null +++ b/app1-setup.js @@ -0,0 +1,21 @@ +// +// 1) Setting up the HTTP server +// + +'use strict'; + +const http = require('http'); +const fs = require('fs'); + +const hostname = 'localhost'; +const port = 3001; + +const server = http.createServer(function(req,res) { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/html'); + res.end('Hello World!\n'); +}); + +server.listen(port, hostname, function () { + console.log(`Listening on http://${hostname}:${port}/`) +}); diff --git a/app2-html.js b/app2-html.js new file mode 100644 index 0000000..b70762c --- /dev/null +++ b/app2-html.js @@ -0,0 +1,28 @@ +// +// 2) Outputting HTML +// + +'use strict'; + +const http = require('http'); +const fs = require('fs'); + +const hostname = 'localhost'; +const port = 3001; + +const server = http.createServer(function(req,res) { + fs.readFile(__dirname + '/public/index.html', 'utf8', function(err,data) { + if (err) { + res.writeHead(404); + res.end("File not found"); + } else { + res.writeHead(200, + {'Content-Type': 'text/html'}); + res.end(data); + }; + }); +}); + +server.listen(port, hostname, function () { + console.log(`Listening on http://${hostname}:${port}/`) +}); diff --git a/app3-json.js b/app3-json.js new file mode 100644 index 0000000..434bc7d --- /dev/null +++ b/app3-json.js @@ -0,0 +1,66 @@ +// +// 3) Displaying request and response data with JSON +// + +'use strict'; + +const http = require('http'); +const fs = require('fs'); + +const hostname = 'localhost'; +const port = 3001; + +const server = http.createServer(function(req,res) { + var reqSnip = { + url: req.url, + method: req.method, + httpVersion: req.httpVersion, + headers: req.headers + }; + + var resSnip = { + statusMessage: res.statusMessage, + statusCode: res.statusCode, + header: res._header + }; + + /* + //this won't work because objects are circular structure, ie, too big + //thus need to "downsize" it + var myJsonReqSnip = JSON.stringify(req, null, 2); + var myJsonResSnip = JSON.stringify(res, null, 2); + */ + + /* + //looks minified + var myJsonReqSnip = JSON.stringify(reqSnip); + var myJsonResSnip = JSON.stringify(resSnip); + */ + + //thus pretty print it + var myJsonReqSnip = JSON.stringify(reqSnip, null, 2); + var myJsonResSnip = JSON.stringify(resSnip, null, 2); + +/* + res.writeHead(200, + //using mime-type = application/json seems to produce errors + {'Content-Type': 'application/json'}); + res.end(myJsonReqSnip + '\n\n' + myJsonResSnip); +*/ + + fs.readFile(__dirname + '/public/index.html', 'utf8', function(err,data) { + if (err) { + res.writeHead(404); + res.end("File not found"); + } else { + res.writeHead(200, + {'Content-Type': 'text/html'}); + data = data.replace('{ req }', myJsonReqSnip).replace('{ res }', myJsonResSnip); + res.end(data); + }; + }); +}); + +server.listen(port, hostname, function () { + console.log(`Listening on http://${hostname}:${port}/`) +}); diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..6b8521a --- /dev/null +++ b/public/index.html @@ -0,0 +1,31 @@ + + +
+{ req }
+
+ { res }
+
+
+
+
+
+