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


<h3>Maddie Rajavasireddy</h3>

Run command `node app.js` to run assignment and view results at http://localhost:3000

Optional part of assignment: Submitting and Processing Post Data

Attempted by printing posting data to console - works
73 changes: 73 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';

let http = require("http");
let fs = require("fs");
let qs = require("querystring");

let host = "localhost";
let port = 3000;

let server = http.createServer(function(req, res) {
// res.writeHead(200, {
// "Content-Type": "text/plain"
// });
// res.end("Hello World!");

fs.readFile("./public/index.html", "utf8", function(err, data){
if (err) {
res.writeHead(404);
res.end("404: Page Not Found");
} else {
res.writeHead(200, {"Content-Type": "text/html"});

// get incoming request data in an object
let reqObj = {
"req.url": req.url,
"req.method": req.method,
"req.httpVersion": req.httpVersion,
"req.headers": req.headers
}

// get response data in an object
let resObj = {
"res.statusCode": res.statusCode,
"res.statusMessage": res.statusMessage,
"res._header": res._header
}

// output JSON objects info into strings
let reqOutput = JSON.stringify(reqObj, null, 2);
let resOutput = JSON.stringify(resObj, null, 2);

// reassign values of html variables to new data string content
data = data.replace("req", reqOutput);
data = data.replace("res", resOutput);

if (req.method === "POST") {

// get posted data
let postObj = "";
req.on("data", function(data) {
postObj += data;
});

// when no more data
req.on("end", function() {

// parse posted data
postObj = qs.parse(postObj);
let postOutput = JSON.stringify(postObj, null, 2);

console.log(`POST Data: ${postOutput}`);
});
}
}

res.end(data);
});

});

server.listen(port, host, function() {
console.log(`Listening at http://${host}:${port}`);
});
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"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": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/maddiereddy/assignment_build_a_nodejs_server.git"
},
"author": "Maddie Rajavasireddy",
"license": "ISC",
"bugs": {
"url": "https://github.com/maddiereddy/assignment_build_a_nodejs_server/issues"
},
"homepage": "https://github.com/maddiereddy/assignment_build_a_nodejs_server#readme"
}
43 changes: 43 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="Building-A-Node-Server Assignment">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>NodeJS Server Test</title>

<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script type="text/javascript" src="scripts.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<h1>Hello NodeJS Server!</h1>

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

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

<section>
<h2>Simple Login Form - GET</h2>
<form action="/" method="GET">
<input type="text" name="username" placeholder="username">
<input type="password" name="password" placeholder="password">
<input type="submit" value="Submit">
</form>
</section>

<section>
<h2>Simple Login Form - POST</h2>
<form action="/" method="POST">
<input type="text" name="username" placeholder="username">
<input type="password" name="password" placeholder="password">
<input type="submit" value="Submit">
</form>
</section>
<br>
<br>
</body>
</html>