diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/app.js b/app.js new file mode 100644 index 0000000..3ed7f5f --- /dev/null +++ b/app.js @@ -0,0 +1,41 @@ +const http = require('http'); +const fs = require('fs'); + +const host = '127.0.0.1'; +const port = 3000; + +var server = http.createServer(function(req, res) { + fs.readFile('./public/index.html', 'utf8', function(err, data) { + if (err) { + res.writeHead(404); + res.end("404 Not Found"); + } else { + res.writeHead(200, { + "Content-Type": "text/html" + }); + + let requested = { + url: req.url, + method: req.method, + httpVersion: req.httpVersion, + headers: req.headers + }; + + let responses = { + statusMessage: res.statusMessage, + statusCode: res.statusCode, + header: res._header + }; + + let modified = data + .replace(/{{ req }}/, JSON.stringify(requested, null, 1)) + .replace(/{{ res }}/, JSON.stringify(responses, null, 1)); + + res.end(modified); + } + }); +}); + +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..21c34a2 --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "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/megantaylor/assignment_build_a_nodejs_server.git" + }, + "author": "megan taylor", + "license": "ISC", + "bugs": { + "url": "https://github.com/megantaylor/assignment_build_a_nodejs_server/issues" + }, + "homepage": "https://github.com/megantaylor/assignment_build_a_nodejs_server#readme", + "dependencies": { + + } +} \ No newline at end of file diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..1ec77a3 --- /dev/null +++ b/public/index.html @@ -0,0 +1,20 @@ + + + +
+ + + +{{ req }}+ +
{{ res }}+ + + + \ No newline at end of file