diff --git a/README.md b/README.md index 29b1d71..ec724ef 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,22 @@ -# assignment_build_a_nodejs_server -Building your first Node.js server and exploring the request and response objects +# Building a Node.js Server + +In this assignment I built my own Node.js server. It dives into serving an HTML page as well as dynamically inserting the data from the request and response objects! + + +## Getting Started + +If you have [installed node](https://nodejs.org/en/download/) on your computer, type the following commands + +``` +$ node app.js +``` + + +## Authors + +* **Dariusz Biskupski** - *Initial work* - https://dariuszbiskupski.com + + +## Acknowledgments + +This assignment is created for [Viking Code School](https://www.vikingcodeschool.com/) diff --git a/app.js b/app.js new file mode 100644 index 0000000..5ff8d84 --- /dev/null +++ b/app.js @@ -0,0 +1,45 @@ +var http = require('http'); +var fs = require('fs'); + +var port = 3000; +var host = 'localhost'; + +var server = http.createServer( function(req, res) { + fs.readFile( './public/index.html', 'utf8', function(err, data) { + if (err) { + res.writeHead(404); + res.end('Page not Found 404'); + } else { + res.writeHead(200, { + "Content-Type": 'text/html' + }); + var requests = { + 'Method is: ': req.method, + 'HTTP Version is : ': req.httpVersion, + 'Request Header is: ': req.headers, + 'URL is: ': req.url + }; + + var responses = { + 'Status Message is: ': res.statusMessage, + 'Status Code is: ': res.statusCode, + 'Response Header is: ': res._header + } + + var strRes = ''; + var strReq = ''; + for(var key in responses) { + strRes += key + JSON.stringify(responses[key], null, 2) + ' \n '; + } + for(var key in requests) { + strReq += key + JSON.stringify(requests[key], null, 2) + ' \n'; + } + var newData = data.replace('{{ res }}', strRes ).replace('{{ req }}', strReq ) + res.end(newData); + } + }) +}); + +server.listen(port, host, function() { + console.log(`Listening at http://${ host }:${ port }/`); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..24e4d60 --- /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": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Visiona/assignment_build_a_nodejs_server.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/Visiona/assignment_build_a_nodejs_server/issues" + }, + "homepage": "https://github.com/Visiona/assignment_build_a_nodejs_server#readme" +} diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..fff79f6 --- /dev/null +++ b/public/index.html @@ -0,0 +1,27 @@ + + + + + + + +

Hello World

+

This is new html page

+

We are testing ....

+ +

Request:

+
{{ req }}
+ +

Response:

+
{{ res }}
+ +
+
+
+
+

+ +
+ + +