diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/README.md b/README.md index 29b1d71..7d168b6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,11 @@ # assignment_build_a_nodejs_server Building your first Node.js server and exploring the request and response objects + + +

Maddie Rajavasireddy

+ +Run command `node app.js` to run assignment and view results at http://localhost:3000 + +Optional part of assignment: Submitting and Processing Post Data + +Attempted by printing posting data to console - works diff --git a/app.js b/app.js new file mode 100644 index 0000000..d8810f6 --- /dev/null +++ b/app.js @@ -0,0 +1,73 @@ +'use strict'; + +let http = require("http"); +let fs = require("fs"); +let qs = require("querystring"); + +let host = "localhost"; +let port = 3000; + +let server = http.createServer(function(req, res) { + // res.writeHead(200, { + // "Content-Type": "text/plain" + // }); + // res.end("Hello World!"); + + 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"}); + + // get incoming request data in an object + let reqObj = { + "req.url": req.url, + "req.method": req.method, + "req.httpVersion": req.httpVersion, + "req.headers": req.headers + } + + // get response data in an object + let resObj = { + "res.statusCode": res.statusCode, + "res.statusMessage": res.statusMessage, + "res._header": res._header + } + + // output JSON objects info into strings + let reqOutput = JSON.stringify(reqObj, null, 2); + let resOutput = JSON.stringify(resObj, null, 2); + + // reassign values of html variables to new data string content + data = data.replace("req", reqOutput); + data = data.replace("res", resOutput); + + if (req.method === "POST") { + + // get posted data + let postObj = ""; + req.on("data", function(data) { + postObj += data; + }); + + // when no more data + req.on("end", function() { + + // parse posted data + postObj = qs.parse(postObj); + let postOutput = JSON.stringify(postObj, null, 2); + + console.log(`POST Data: ${postOutput}`); + }); + } + } + + res.end(data); + }); + +}); + +server.listen(port, host, function() { + console.log(`Listening at http://${host}:${port}`); +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..3e60f4c --- /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": "app.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/maddiereddy/assignment_build_a_nodejs_server.git" + }, + "author": "Maddie Rajavasireddy", + "license": "ISC", + "bugs": { + "url": "https://github.com/maddiereddy/assignment_build_a_nodejs_server/issues" + }, + "homepage": "https://github.com/maddiereddy/assignment_build_a_nodejs_server#readme" +} diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..55e9517 --- /dev/null +++ b/public/index.html @@ -0,0 +1,43 @@ + + + + + + + + NodeJS Server Test + + + + + + +

Hello NodeJS Server!

+ +

Request:

+
{{ req }}
+ +

Response:

+
{{ res }}
+ +
+

Simple Login Form - GET

+
+ + + +
+
+ +
+

Simple Login Form - POST

+
+ + + +
+
+
+
+ + \ No newline at end of file