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

Steven Zarrella
39 changes: 39 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";

const http = require('http'),
fs = require('fs');

let 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"
});

let reqArray = ["url", "method", "httpVersion", "headers"],
reqObjectArray = [],
resArray = ["statusMessage", "statusCode", "_header"],
resObjectArray = [];

for (var item in reqArray) {
reqObjectArray.push({[reqArray[item]]: req[reqArray[item]]});
};

for (var item in resArray) {
resObjectArray.push({[resArray[item]]: res[resArray[item]]});
};

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

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

server.listen(3000, 'localhost', function() {
console.log("Listening at http://localhost:3000");
});
22 changes: 22 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Build a Node.js server</title>
</head>
<body>
<h1>Meow World!</h1><br>
<form action="/" method="get">
<p>username: <input type="text" name="username"></p>
<p>password: <input type="password" name="password"></p>
<input type="submit">
</form>

<h2>Request:</h2>
<pre>{{ req }}</pre><br>

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

</body>
</html>