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
38 changes: 38 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

let http = require('http');
let fs = require('fs');

let port = 3000;
let host = 'localhost';

let wantedReqProperties = ["url", "method", "httpVersion", "headers"];
let wantedResProperties = ["statusMessage", "statusCode", "_header"];

let server = http.createServer((req, res) => {
fs.readFile('./public/index.html', 'utf8', (err, data) => {
if (err) {
res.writeHead(404);
res.end("404 Not Found");
} else {
res.writeHead(200, {
"Content-Type":"text/html"
});

let request = wantedReqProperties.map((property) => {
return { [property] : req[property] };
});
let response = wantedResProperties.map((property) => {
return { [property] : res[property] };
});

let newData = data.replace("{{ req }}", JSON.stringify(request))
.replace("{{ res }}", JSON.stringify(response));

res.end(newData);
}
});
});

server.listen(port, host, () => {
console.log(`http://${host}:${port}`);
});
30 changes: 30 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>
<head></head>
<body>
<h1>This HTML is being served by node</h1>
<h2>Request: </h2>
<pre> {{ req }} </pre>

<h2>Response: </h2>
<pre> {{ res }} </pre>
<!-- Here follows some extremely lazy markup -->
<br>
<br>
<form action = "/" method = "get">
<fieldset>
<legend> Name </legend>
<input type = "text" name = "Name">
<br>
<br>
<legend> Password </legend>
<input type = "password" name = "Password">
<br>
<br>
<input type = "submit" value = "Submit">
</fieldset>
</form>

</body>
</html>