Provides basic utility functions related to the operating system. 2 path Module
Provides utilities to handle and transform the path to the file. 3 net Module
Works as Network Wrapper, turns Server and Client into Streams. 4 dns Module
Provides functions to perform DNS Lookup. 5 domain Module
Used to interfere with unresolved errors
The OS Module in Node.js provides basic operating system-related functions. This module can be imported by the following syntax:
var os = require ( "os" )
Method of the os Module in Node.js
Stt Method & Description1 os.tmpdir ()Properties of the os Module in Node.js
os.EOL : A End-Of-Line definition of the operating system.
For example
Below is an example to illustrate some methods of the os Module in Node.js. You create main.js with the following content:
var os = require ( "os" ); // Print information about OS console . log ( 'OS Type la:' + os . type ()); // Print information on OS platform console . log ( 'OS Platform:' + os . platform ()); // Print information about the console . log ( ' Application :' + os . totalmem () + "byte." ); // Print information about the console console . log ( 'The file is whip:' + os . freemem () + "byte." );
Run main.js to see the result ::
$ node main . js
Check the result:
OS Type la: Linux
OS Platform: linux
Tongue size: 25103400960 bytes.
Blade size: 20676710400 bytes.
The path Module in is used to Resolve and convert the path to the file. This module can be inport according to the syntax:
var path = require ( "path" )
Method of path Module in Node.js
Method & Description1 path.normalize (p)Properties of path Module in Node.js
Stability Properties & Description1 path.sepFor example
The following example illustrates how to use some methods and properties of the Path Module in Node.js. You create main.js with the following content:
var path = require ( "path" ); // Check out the normalize () console . log ( ' Method th NORMALIZE:' + path . normalize ( '/ test / test1 // 2slashes / 1slash / tab / .' )); // Ket hop the following information to create a console . log ( 'Method of JOIN:' + path . join ( '/ test' , 'test1' , '2slashes / 1slash' , 'tab' , '.' )); // Resolve a console game . log ( 'Phuong thuc RESOLVE:' + path . resolve ( 'main.js' )); // Lay out the information on the console . log ( 'Phuong thuc EXTNAME:' + path . extname ( 'main.js' ));
Run main.js to see the result ::
$ node main . js
Check the result.
Method of NORMALIZE: / test / test1 / 2slashes / 1slash
Phuong thuc JOIN: / test / test1 / 2slashes / 1slash
Phuong thuc RESOLVE: /web/com/1427176256_27423/main.js
Phuong thuc EXTNAME: .js
Net Module in Node.js is used to create Server and Client. This module provides an asynchronous Network Wrapper and can be imported with the syntax:
var net = require ( "net" )
The net.Server class is used to create a TCP Server or Local Server.
Method of net.Server class in Node.js
Method & Description1 server.listen (port [, host] [, backlog] [, callback])The event of net.Server class in Node.js
No. Events & Description1 listeningThis object is an abstract class of TCP or Local Socket. net.Socket inherits the duplex Stream interface. They can be created by the user or by a Client (by the connect () method) or can be created by Node.js and transmitted to the user via the 'connection' event of a Server.
Net.Socket event in Node.js
net.Socket is an eventEmitter and it generates the following events.
Stt Events & Description1 lookupNet.Socket property in Node.js
net.Socket has many useful properties to help you better control interacting with Socket.
Socket.bufferSize1 and Description1Net.Socket method in Node.js
Method & Description1 new net.Socket ([options])For example
Below is an example to illustrate some methods and properties of net Module in Node.js.
Create server.js with content:
var net = require ( 'net' ); var server = net . createServer ( function ( connection ) { console . log ( 'Client connection ' ); connection . on ( 'end' , function () { console . log ( 'Client login ' ); }); connection . ( 'Hello World! Rn' ); connection . Pipe ( connection ); }); server . listen ( 8080 , function () { console . log ( 'Server dang lang' ); });
Run server.js to see the result:
$ node server . js
Check the result.
Server is listening
Next, create client.js as shown below
var net = require ( 'net' ); var client = net . connect ({ port : 8080 }, function () { console . log ( 'Da ket noi elephant Server!' ); }); client . on ( 'data' , function ( data ) { console . log ( data . toString ()); client . end (); }); client . on ( 'end' , function () { console . log ( 'Mat server and server' ); });
Run client.js on another terminal screen to see the result:
$ node client . js
Check the result.
Da ket noi Server!
Hello World!
Please contact the server Elephant
Check the results on the Terminal screen running server.js:
Server is listening
Ket said the Client
Please contact Client
dns Module in Node.js is used to perform DNS Lookup. The module provides an asynchronous Network Wrapper and can be imported with syntax.
var dns = require ( "dns" )
The list below lists the rrtypes values used by the dns.resolve () method:
A - The default value is IPV4 addresses
AAAA - IPV6 addresses
MX - Mail Exchange logs
TXT - Text records
SRV - Records of SRV
PTR - Used to reverse IP Lookup
NS - Records of Name Server
CNAME - Records about Canonical Name
SOA - stands for Start of Authority Record
Each DNS trace can return one of the following Error Code:
dns.NODATA - The DNS Server returns a response that does not include any data.
dns.FORMERR - DNS Server notifies wrong format query.
dns.SERVFAIL - DNS Server returns a common error.
dns.NOTFOUND - Domain name not found.
dns.NOTIMP - DNS Server does not implement request activity.
dns.REFUSED - The DNS Server rejects the query.
dns.BADQUERY - Wrong format query.
dns.BADNAME - Hostname wrong format.
dns.BADFAMILY - Address Family is not supported.
dns.BADRESP - Feedback from DNS is incorrect.
dns.CONNREFUSED - Cannot connect to DNS Server.
dns.TIMEOUT - Timeout while connecting to the DNS Server.
dns.EOF - Short of End of file.
dns.FILE - An error occurred while reading the file.
dns.NOMEM - Out of memory.
dns.DESTRUCTION - Channel is being canceled.
dns.BADSTR - Wrong format string.
dns.BADFLAGS - The flags are invalid.
dns.NONAME - Hostname is not in digital form.
For example
The following example illustrates some methods of dns Module in Node.js. You create main.js with the following content:
var dns = require ( 'dns' ); dns . lookup ( 'www.google.com' , function onLookup ( err , address , family ) { console . log ( 'Dia chi:' , address ); dns . reverse ( address , function ( err , hostnames ) { if ( err ) { console . log ( err . stack ); } console . log ( 'Method of REVERSE for dia:' + address + 'bar with hostname:' + JSON . stringify ( hostnames )); }); });
Run main.js to see the result ::
$ node main . js
Check the result:
Dia spent: 173.194.46.83
Method of REVERSE for branch 173,194.46.83 bar with hostname: ["ord08s11-in-f19.1e100.net"]
The Module domain in Node.js is used to intervene unresolved errors. These errors can be interfered by using Internal Binding or External Binding.
Internal Binding - Error Emmitter is executing its code inside a Domain's run method.
External Binding - Error Emmitter is added to a Domain using the add method.
To use the Module domain in Node.js, you need to import the following syntax.
var domain = require ( "domain" )
The Domain class of the Domain Module is used to provide functionality to route Error and Exception to a Domain object. This class is the subclass of the EventEmitter class. This Domain class handles errors that it captures and listens for its error events. This object can be created by syntax:
var domain = require ( "domain" ); var child = domain . create ();
Method of the Domain Module in Node.js
Stt Method & Description1 domain.run (function)Properties of the Domain Module in Node.js
domain.members: An array of Timer and Event Emitter has been added to the domain.
For example
The following example illustrates some methods of the Domain Module in Node.js. You create main.js with the following content:
var EventEmitter = require ( "events" ). EventEmitter ; var domain = require ( "domain" ); var emitter1 = new EventEmitter (); // Create a domain to use the software to create () var domain1 = domain . create (); domain1 . on ( 'error' , function ( err ) { console . log ( "domain1 will be released: (" + err . message + ")" ); }); // I have an Emitter with a domain name to add () domain1 . add ( emitter1 ); emitter1 . on ( 'error' , function ( err ) { console . log ( "listener se xu ly: (" + err . message + ")" ); }); emitter1 . emit ( 'error' , new Error ( ' Error listener' ); emitter1 . removeAllListeners ( 'error' ); emitter1 . emit ( 'error' , new Error ( 'Duoc domain1' ); var domain2 = domain . create (); domain2 . on ( 'error' , function ( err ) { console . log ( "domain2 will be free: (" + err . message + ")" ); }); / / I have an emitter with domain name to run () domain2 . run ( function () { var emitter2 = new EventEmitter (); emitter2 . emit ( 'error' , new Error ( 'Duoc domain2' )); }); domain1 . remove ( emitter1 ); emitter1 . emit ( 'error' , new Error ( 'Exception'. He thong bi pha vo! ' ));
Run main.js to see the result ::
$ node main . js
Check the result:
listener will be free: (Listener listener)
domain1 will make a difference: (Can I use domain1)
domain2 will earn a lot of money: (Can not use domain2)
events.js: 72
throw er; // Unhandled 'error' event
^
Error: Exception. He is bouncing!
at Object. (/web/com/1427722220_30772/main.js:40:24)
at Module._compile (module.js: 456: 26)
at Object.Module._extensions.js (module.js: 474: 10)
at Module.load (module.js: 356: 32)
at Function.Module._load (module.js: 312: 12)
at Function.Module.runMain (module.js: 497: 10)
at startup (node.js: 119: 16)
at node.js: 906: 3
According to Tutorialspoint
Last lesson: Global objects in Node.js
Next lesson: Web Module in Node.js