Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created simple web server #54

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
# assignment_build_a_nodejs_server
Building your first Node.js server and exploring the request and response objects
# Building a Node.js Server

In this assignment I built my own Node.js server. It dives into serving an HTML page as well as dynamically inserting the data from the request and response objects!


## Getting Started

If you have [installed node](https://nodejs.org/en/download/) on your computer, type the following commands

```
$ node app.js
```


## Authors

* **Dariusz Biskupski** - *Initial work* - https://dariuszbiskupski.com


## Acknowledgments

This assignment is created for [Viking Code School](https://www.vikingcodeschool.com/)
45 changes: 45 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var http = require('http');
var fs = require('fs');

var port = 3000;
var host = 'localhost';

var server = http.createServer( function(req, res) {
fs.readFile( './public/index.html', 'utf8', function(err, data) {
if (err) {
res.writeHead(404);
res.end('Page not Found 404');
} else {
res.writeHead(200, {
"Content-Type": 'text/html'
});
var requests = {
'Method is: ': req.method,
'HTTP Version is : ': req.httpVersion,
'Request Header is: ': req.headers,
'URL is: ': req.url
};

var responses = {
'Status Message is: ': res.statusMessage,
'Status Code is: ': res.statusCode,
'Response Header is: ': res._header
}

var strRes = '';
var strReq = '';
for(var key in responses) {
strRes += key + JSON.stringify(responses[key], null, 2) + ' \n ';
}
for(var key in requests) {
strReq += key + JSON.stringify(requests[key], null, 2) + ' \n';
}
var newData = data.replace('{{ res }}', strRes ).replace('{{ req }}', strReq )
res.end(newData);
}
})
});

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": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Visiona/assignment_build_a_nodejs_server.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/Visiona/assignment_build_a_nodejs_server/issues"
},
"homepage": "https://github.com/Visiona/assignment_build_a_nodejs_server#readme"
}
27 changes: 27 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Hello World</h1>
<h3>This is new html page</h3>
<p>We are testing ....</p>

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

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

<form class="" action="index.html" method="GET">
<label for="username">Username:</label><br>
<input type="text" name="username" value=""><br>
<label for="password">Password:</label><br>
<input type="text" name="password" value=""><br><br>
<input type="submit" name="submit" value="Sign Up">
</form>

</body>
</html>