Web Module in Node.js
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:
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.
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
You should read it
May be interested
- What is DIMM (Dual In-line Memory Module)?dimm is a printed circuit board with integrated dram or sdram integrated circuits. let's find out more about dimms through the following article.
- How to check if the Windows PC has a Trusted Platform Module (TPM) chipthis guide will show you how to check if your windows pc has the trusted platform module (tpm) security hardware chip, as well as see the tpm details and version, if any.
- Function sleep () in Pythonmodule time in python provides some very useful functions to handle time-related tasks. one of the most commonly used functions is sleep ().
- Google launched the Developers Kit Module for Project Aragoogle has just officially released the first developers kit of project ara for developers. through this set of mdks, google wants to help developers get a grasp of what to do to create replaceable parts for the upcoming puzzle phone.
- Raspberry Pi platform first introduces 12 MP camera module with interchangeable lensa 12mp camera module has been released by the raspberry pi for diy enthusiasts.
- How to remove the Magisk module using TWRP Recoverya quick guide to uninstalling the magisk module without using magisk manager is also known as uninstalling the magisk module using twrp recovery or offline.
- What is VRM and how does it affect the performance of the processorthe voltage regulator module (voltage regulator module - vrm) is one of the important hardware components in the general structure of a motherboard system.
- Module in Pythonin this python lesson we will know how to create and import a module in python. we will learn a few ways to import, using custom modules as well as python's built-in modules, please watch.
- Warning of Critical Vulnerability Affecting Realtek Wi-Fi Modulesthrough these vulnerabilities, malicious actors can completely gain higher privileges on the target device.
- Apple puts cameras on Airpodsaccording to analyst ming-chi kuo, apple will mass produce airpods with an infrared camera module similar to the face id module on iphone and ipad in 2026.