Node.js Web Module
What is a Web Server?
A web server is generally referred to as a website server. It is a program residing on a computer of a certain type on the internet. The basic function of a web server is to provide web information browsing services. It only needs to support the HTTP protocol, HTML document format, and URL, and works in conjunction with the client's web browser.
Most web servers support server-side scripting languages (such as PHP, Python, Ruby, etc.), and these scripting languages retrieve data from databases and return the results to the client's browser.
The three most popular web servers currently are Apache, Nginx, and IIS.
Web Application Architecture
-
Client - The client, typically a browser, can request data from the server via the HTTP protocol.
-
Server - The server, typically a web server, can receive client requests and send response data to the client.
-
Business - The business layer, processes applications via the web server, such as interacting with databases, performing logical operations, and calling external programs.
-
Data - The data layer, typically consisting of databases.
Creating a Web Server with Node
Node.js provides an http module, which is primarily used to build HTTP servers and clients. The http module must be used to enable HTTP server or client functionality. The code is as follows:
var http = require('http');
The following demonstrates a basic HTTP server architecture (using port 8080). Create a file named server.js
with the following code:
Example
var http = require('http');
var fs = require('fs');
var url = require('url');
// Create server
http.createServer(function (request, response) {
// Parse the request, including the file name
var pathname = url.parse(request.url).pathname;
// Output the requested file name
console.log("Request for " + pathname + " received.");
// Read the requested file content from the file system
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
console.log(err);
// HTTP Status: 404 : NOT FOUND
// Content Type: text/html
response.writeHead(404, {'Content-Type': 'text/html'});
} else {
// HTTP Status: 200 : OK
// Content Type: text/html
response.writeHead(200, {'Content-Type': 'text/html'});
// Response file content
response.write(data.toString());
}
// Send the response data
response.end();
});
}).listen(8080);
// Console will output the following information
console.log('Server running at http://127.0.0.1:8080/');
Next, create an index.html
file in the same directory with the following code:
index.html File
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
Execute the server.js
file:
$ node server.js
Server running at http://127.0.0.1:8080/
Then, open the address http://127.0.0.1:8080/index.html in your browser, which will display as shown below:
The console output when executing server.js
is as follows:
Server running at http://127.0.0.1:8080/
Request for /index.html received. # Client request information
Creating a Web Client with Node
To create a web client with Node, you need to include the http module and create a client.js file with the following code:
Example
var http = require('http');
// Options for the request
var options = {
host: 'localhost',
port: '8080',
path: '/index.html'
};
// Callback function to handle the response
var callback = function(response){
// Continuously update the data
var body = '';
response.on('data', function(data) {
body += data;
});
response.on('end', function() {
// Data reception complete
console.log(body);
});
}
// Send a request to the server
var req = http.request(options, callback);
req.end();
Open a new terminal and execute the client.js file. The output will be as follows:
$ node client.js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
The console output for executing server.js will be as follows:
Server running at http://127.0.0.1:8080/
Request for /index.html received. # Client request information