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

const host = '127.0.0.1';
const port = 3000;

var server = http.createServer(function(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 requested = {
url: req.url,
method: req.method,
httpVersion: req.httpVersion,
headers: req.headers
};

let responses = {
statusMessage: res.statusMessage,
statusCode: res.statusCode,
header: res._header
};

let modified = data
.replace(/{{ req }}/, JSON.stringify(requested, null, 1))
.replace(/{{ res }}/, JSON.stringify(responses, null, 1));

res.end(modified);
}
});
});

server.listen(port, host, function() {
console.log(`Listening at http://${ host }:${ port }`);
});
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "assignment_build_a_nodejs_server",
"version": "1.0.0",
"description": "Building your first Node.js server and exploring the request and response objects",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/megantaylor/assignment_build_a_nodejs_server.git"
},
"author": "megan taylor",
"license": "ISC",
"bugs": {
"url": "https://github.com/megantaylor/assignment_build_a_nodejs_server/issues"
},
"homepage": "https://github.com/megantaylor/assignment_build_a_nodejs_server#readme",
"dependencies": {

}
}
20 changes: 20 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>

<body>

<form action="#" method="get">
<input type="text" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password">
<input type="submit" value="Submit">
</form>

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

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

</body>

</html>