Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# assignment_build_a_nodejs_server
Building your first Node.js server and exploring the request and response objects

Tzvi Seliger
23 changes: 23 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -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}` );
});

26 changes: 26 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Hello World</h1>
<h2>Request:</h2>

<pre>
{{ req }}
</pre>

<h2>Response:</h2>

<pre>
{{ res }}
</pre>
<form action = '../app.js' method = 'get'>
<input type = 'text' name = "username">
<input type = 'password' name = "password">
<input type = 'submit'>
</form>
</body>
</html>