Skip to content

Commit 4bf0b9f

Browse files
committed
Web Module
1 parent d1f6a82 commit 4bf0b9f

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

nodejs-basics/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
6+
<title>NodeJS Web Server</title>
7+
<meta name="description" content="The HTML5 Herald">
8+
<meta name="author" content="SitePoint">
9+
10+
</head>
11+
12+
<body>
13+
NodeJS Web Module Example!
14+
</body>
15+
</html>

nodejs-basics/web-module.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Web Module
3+
*
4+
* Web Server is a software program that handles HTTTP requests sent by HTTP clients
5+
* like web browsers, and returns web pages in response to the clients. Web servers
6+
* usually respond with html documents along with images, style sheets and scripts.
7+
*
8+
*
9+
* ------------------------------
10+
* Web Application Architecture
11+
* ------------------------------
12+
*
13+
* 1. Client Layer: The Client layer contains web browsers, mobile browsers or applications
14+
* which can make HTTP request to the web server.
15+
*
16+
* 2. Server Layer: The Server layer contains Web server which can intercepts the request made
17+
* by clients and pass them the response.
18+
*
19+
* 3. Business Layer: The business layer contains application server which is utilized by web
20+
* server to do required processing. This layer interacts with data layer via data base or some external programs.
21+
*
22+
* 4. Data Layer: The Data layer contains databases or any source of data.
23+
*
24+
*/
25+
26+
27+
var http = require('http');
28+
var fs = require('fs');
29+
var url = require('url');
30+
31+
// Create a server
32+
http.createServer( function (request, response) {
33+
// Parse the request containing file name
34+
var pathname = url.parse(request.url).pathname;
35+
36+
// Print the name of the file for which request is made.
37+
console.log("Request for " + pathname + " received.");
38+
39+
// Read the requested file content from file system
40+
fs.readFile(pathname.substr(1), function (err, data) {
41+
if (err) {
42+
console.log(err);
43+
// HTTP Status: 404 : NOT FOUND
44+
// Content Type: text/plain
45+
response.writeHead(404, {'Content-Type': 'text/html'});
46+
}else{
47+
//Page found
48+
// HTTP Status: 200 : OK
49+
// Content Type: text/plain
50+
response.writeHead(200, {'Content-Type': 'text/html'});
51+
52+
// Write the content of the file to response body
53+
response.write(data.toString());
54+
}
55+
// Send the response body
56+
response.end();
57+
});
58+
}).listen(4200);
59+
// Console will print the message
60+
console.log('Server running at http://localhost:4200/index.html');

0 commit comments

Comments
 (0)