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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# assignment_build_a_nodejs_server
Building your first Node.js server and exploring the request and response objects


Jennifer Louie
45 changes: 45 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const http = require('http');
const fs = require('fs');

const port = 3000;
const host = 'localhost';

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

let shallowViewRequest = createViewReq(req);
let shallowViewResponse = createViewRes(res);
let newHtml = data.replace('{{ req }}', JSON.stringify(shallowViewRequest));
newHtml = newHtml.replace('{{ res }}', JSON.stringify(shallowViewResponse));
res.end(newHtml);
}
});
});

server.listen(port, host, function() {
console.log(`Listening at http://${ host }:${ port }`);
});

function createViewReq(req) {
return {
url : req.url,
method : req.method,
httpVersion : req.httpVersion,
headers : req.headers
};
}

function createViewRes(res) {
return {
statusMessage : res.statusMessage,
statusCode : res.statusCode,
_header : res._header
};
}
Loading