diff --git a/README.md b/README.md index 29b1d71..4b7d3cd 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 + +Tzvi Seliger diff --git a/app.js b/app.js new file mode 100644 index 0000000..ec68a12 --- /dev/null +++ b/app.js @@ -0,0 +1,23 @@ +let http = require('http'); +let fs = require('fs'); + +let port = 3000; +let host = 'localhost'; + +let server = http.createServer((req, res) => { + fs.readFile('./public/index.html', 'utf-8', function( err, data ) { + + res.writeHead (200, {'Content-Type': 'text/html' }); + + res.end( + data + .replace(/{{ res }}/, JSON.stringify(res.statusMessage+" "+res.statusCode+" "+res._header, null, 2) ) + .replace(/{{ req }}/, JSON.stringify(req.url+" "+req.method+" "+req.httpVersion+" "+req.headers, null, 2)) + ); + }); +}) + +server.listen(port, host, function(){ + console.log( `Server Running at ${host} ${port}` ); +}); + diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..a27e021 --- /dev/null +++ b/public/index.html @@ -0,0 +1,26 @@ + + + + + + + +

Hello World

+

Request:

+ +
+		{{ req }}
+	
+ +

Response:

+ +
+		{{ res }}
+	
+
+ + + +
+ + \ No newline at end of file