To learn more about Utility Module and Web Module in Node.js, readers can refer to the article 'Learning Node.js' below by Mytour.
Utility Module in Node.js provides utility functions for common tasks, while the Web Module is used for creating web servers in Node.js.
Node.js module library includes several Utility modules, which are quite popular and widely used for developing Node-based applications.
Below is a list of Utility Modules in Node.js:
The Web Module in Node.js provides functionality for creating web servers.
A Web Server is a software program that specializes in handling HTTP requests sent by HTTP clients such as web browsers and returning web pages to respond to clients.
A Web Server is a software program that specializes in handling HTTP requests sent by HTTP clients such as web browsers and returning web pages to respond to clients. Web servers typically respond with HTML documents along with images, spreadsheets, and scripts.
Most Web Servers support server-side scripting using script languages or redirect to application servers to perform specific tasks such as fetching data from databases, executing complex logic, ... and then send the results to the HTTP client through the Web server.
Apache web server is one of the most widely used web servers today. It is an open-source project.
The Web Application Structure consists of 4 layers: Client Layer, Server Layer, Business Layer, and Data Layer.
A web application consists of 4 layers:
- Client Layer: The Client Layer contains web browsers, mobile browsers, or applications that can make HTTP requests to the web server.
- Server Layer: The Server Layer contains the Web server that can intercept client requests and send responses back.
- Business Layer: The Business Layer contains the application server used by the web server to process requests. This layer interacts with the data layer through a database or some external programs.
- Data Layer: The Data Layer contains a database or any data source.
How to Create a Web Server Using Node.js
You can use Node.js to create a server-side HTTP server. Node.js provides the HTTP module, which is mainly used to build HTTP servers.
In the example below, Mytour will guide you on how to write code to create an HTTP server listening on port 3030:
First, create a js file named server.js containing the following code:
Next, create an HTML file named index.html containing the following code in the same directory where the server.js file is created:
Open Node.js command prompt and run the following code:
C:\Users\system name\Desktop\Node_js\node server.js
Finally, open http://localhost:3030/index.html in any web browser on your computer and see the result:
Explanation of the code:
1. Import necessary modules: Use the require directive to load the available node.js modules in the integrated node.js applications. To use modules or import modules into the program, you must import specific modules at the beginning of the program through the require object.
2. Create server: Next, call the createServer() method to create an HTTP server in the program. We will define a function that takes 2 parameters request and response.
3. Analyze the downloaded UR syntax.
4. Use the fs.readFile() method to read the content of the requested file from the system.
5. Create a header containing the content as text or HTML. If an error occurs, then the server will generate a 404 code, meaning the server did not find the file at the specific location.
6. The next step is to respond to the server, using the res.writeHead() method to define the HTTP status: 200, meaning the server found the file. Then determine the content type, meaning the file type you are using can be text or HTML.
7. Finally, determine the location and corresponding port of the file, in this example the port number is 3030.
Above, Mytour has just introduced you to what Utility Module and Web Module are in Node.js? If you have any questions or need clarification on topics like Buffer in node.js, feel free to leave your comments below the article, Mytour will answer your questions as soon as possible.
