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
53 changes: 53 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const http = require("http");
const fs = require("fs");

const host = "127.0.0.1";
const port = 3000;

var server = http.createServer((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" });

var reqObject = {
url: req.url,
method: req.method,
httpVersion: req.httpVersion,
headers: req.headers
};
var resObject = {
statusMessage: res.statusMessage,
statusCode: res.statusCode,
_header: res._header
};

var reqString = JSON.stringify(reqObject, null, 2);

var resString = JSON.stringify(resObject, null, 2);

data = data.replace(/{ req }/i, reqString);
data = data.replace(/{ res }/i, resString);
res.end(data);
}
});
});

server.listen(port, host, function() {
console.log(
`The server is now running and listening at the address http://${host}:${port}`
);
});

/*
req.url
req.method
req.httpVersion
req.headers

res.statusMessage
res.statusCode
res._header
*/
31 changes: 31 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>My First Node.js Server</title>

<body>
<h1>This is a heading in My First Node.js Server</h1>
<h2>Request:</h2>
<pre>{{ req }}</pre>

<h2>Response:</h2>
<pre>{{ res }}</pre>

<form action="./app.js" method="get">
<label>
Username
<input id="name" value="username" type="text">
</label>
<br>
<label>Password
<input id="password" value="password" type="password">
</label>
<br>
<button type="submit">Submit</button>
</form>
</body>

</html>