Web Module in Node.js

Web Server is a software application that can handle HTTP requests sent by HTTP Client (for example, a web browser) and return a web page in response to the Client. Web Server often sends html documents next to images as well as style sheets and Javascript segments.

What is Web Server?

Web Server is a software application that can handle HTTP requests sent by HTTP Client (for example, a web browser) and return a web page in response to the Client. Web Server often sends html documents next to images as well as style sheets and Javascript segments.

Web application structure

A web application is usually divided into 4 classes as follows:

Web Module in Node.js Picture 1

Client - This class includes web browsers and applications that can generate HTTP requests to the Web Server.

Server - This class includes Web Servers that can interfere with requests made by the Client and return responses (response).

Business - This class includes applications on the Server that can be exploited by Web Servers to perform the necessary processing. This class interacts with Data class through external programs.

Data - This class includes Database and any data sources.

Create Web Server using Node.js

Node.js provides http Module that can be used to create HTTP clients and servers. Below is the miniature architecture part of HTTP Server listened to on port 8081.

First, you create server.js with the following content:

 var http = require ( 'http' ); var fs = require ( 'fs' ); var url = require ( 'url' ); // Create an http server . createServer ( function ( request , response ) { // Parse request has the file var pathname = url . parse ( request . url ). pathname ; // Print the information file of the file created by the Request . "Request for" + pathname + "skin." // // Doc file using the file requires Request fs readFile ( pathname . Substr ( 1 ), function ( err , data ) { if ( err ) { console . log ( err ); // HTTP Status: 404: NOT FOUND // Content Type: text / plain response . writeHead ( 404 , { 'Content-Type' : 'text / html' }); } else { / / HTTP Status: 200: OK // Content Type: text / plain response . WriteHead ( 200 , { 'Content-Type' : 'text / html' }); // Write file of BODY to be locked Response response . write ( data . toString ()); } // Gui phan BODY close Response response . end (); }); }). listen ( 8081 ); // Print information after the console console . log ( 'Server is running: http://127.0.0.1:8081/' ); 

Now create index.htm with the same directory as server.js:

 Vi du Web Module trong Node.js Hello World! 

Run server.js to see the result:

 $ node server . js 

Check the result

 Server is currently running: http://127.0.0.1:8081/ 

Create Request to Node.js Server

Open http://127.0.0.1:8081/index.htm in any browser and see the results.

Web Module in Node.js Picture 2

Check the result:

 Server is currently running: http://127.0.0.1:8081/ 
Request for /index.htm is available.

Create Web Client using Node.js

To create a Web Client, you can also use http Module. Here is an example.

Create client.js with the following content:

 var http = require ( 'http' ); // Tao doi tuong options to use Request var options = { host : 'localhost' , port : '8081' , path : '/index.htm' }; // Callback can be used to respond Response var callback = function ( response ) { // Update the data to Stream var body = '' ; response . on ( 'data' , function ( data ) { body + = data ; }); response . on ( 'end' , function () { // Ket via the user console . console . log ( body ); }); } // Create a request toi Server var req = http . request ( options , callback ); req . end (); 

Run client.js on another Terminal screen to see the result:

 $ node client . js 

Check the result:


Travel Web Module in Node.js

Hello World!

Then check the results on the Server.

 Server is currently running: http://127.0.0.1:8081/ 
Request for /index.htm is available.

According to Tutorialspoint

Previous article: Utility Module in Node.js

Next article: Express Framework in Node.js

4.5 ★ | 2 Vote | 👨 2095 Views
« PREV POST
NEXT POST »