Node.js | Utility Module

Node.js Utility Module: The Util module in node.js provides access to various utility functions.

Syntax:

var util = require('util');

There are various utility modules available in node.js module library. The modules are extremely useful in developing node based web applications.
Various utility modules present in node.js are as follows:



OS Module: Operating System based utility modules for node.js are provided by OS module.

Syntax:

var os = require('os');

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Require operating System module
var os = require("os"); 
  
// Display operating System type
 console.log('Operating System type : ' + os.type()); 
  
 // Display operating System platform
 console.log('platform : ' + os.platform()); 
  
 // Display total memory
 console.log('total memory : ' + os.totalmem() + " bytes."); 
  
 // Display available memory 
console.log('Available memory : ' + os.availmem() + " bytes.");

chevron_right


Output:

Path Module: Path module in node.js is used for transforming and handling various file paths.

Syntax:

var path = require('path');

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Require path
var path = require('path');
  
// Display Resolve
console.log('resolve:' + path.resolve('paths.js')); 
  
// Display Extension
console.log('extension:' + path.extname('paths.js'));

chevron_right


Output:

DNS Module: DNS Module enables us to use the underlying Operating System name resolution functionalities. The actual DNS lookup is also performed by the DNS Module. This DNS Module provides an asynchronous network wrapper. The DNS Module can be imported using the below syntax.

Syntax:



var dns = require('dns');

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Require dns module
const dns = require('dns'); 
  
// Store the web address
const website = 'www.geeksforgeeks.org';
  
// Call lookup function of DNS
dns.lookup(website, (err, address, family) => { 
    console.log('Address of %s is %j family: IPv%s',
            website, address, family); 
}); 

chevron_right


Output:

Address of www.geeksforgeeks.org is "203.92.39.72"
family: IPv4

Net Module: Net Module in node.js is used for the creation of both client and server. Similar to DNS Module this module also provides an asynchronous network wrapper.

Syntax:

var net = require('net');

Example: This example containing the code for server side.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Require net module
var net = require('net'); 
  
var server =  net.createServer(function(connection) { 
  console.log('client connected'); connection.on('end', function() { 
    console.log('client disconnected');
  }); 
  connection.write('Hello World!\r\n'); connection.pipe(connection); 
});
  
server.listen(8080, function() { 
  console.log('server  listening');
});

chevron_right


Output:

Server listening

Example: This example is containing the for client side in net Module.

filter_none

edit
close

play_arrow

link
brightness_4
code

var net = require('net');
  
var client = net.connect(8124, function() { 
  console.log('Client Connected');
  client.write('GeeksforGeeks\r\n');
});
  
client.on('data', function(data) {
  console.log(data.toString());
  client.end();
});
  
client.on('end', function() {
  console.log('Server Disconnected');
});

chevron_right


Output:

Client Connected 
GeeksforGeeks
Server Disconnected

Domain Module: Domain module in node.js is used to intercept unhandled errors. The interception can be performed by either:

  • Internal Binding: Error emitter executes the code internally within the run method of a domain.
  • External Binding: Error emitter is added explicitly to the domain using the add method.

Syntax:

var domain = require('domain');

The domain class in the domain module provides a mechanism of routing the unhandled exceptions and errors to the active domain object. It is considered to be the child class of EventEmitter.

Syntax:

var domain = require('domain');
var child = domain.create();

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

var EventEmitter = require("events").EventEmitter; 
var domain = require("domain");
var emit_a = new EventEmitter();
var dom_a = domain.create(); 
  
dom_a.on('error', function(err) { 
  console.log("Error handled by dom_a ("+err.message+")"); 
});
  
dom_a.add(emit_a);
emit_a.on('error', function(err) { 
  console.log("listener handled this error ("+err.message+")"); 
});
  
emit_a.emit('error', new Error('Listener handles this')); 
emit_a.removeAllListeners('error'); 
emit_a.emit('error', new Error('Dom_a handles this')); 
var dom_b = domain.create(); 
  
dom_b.on('error', function(err) {
  console.log("Error handled by dom_b ("+err.message+")"); 
});
  
dom_b.run(function() {
  var emit_b = new EventEmitter(); 
  emit_b.emit('error', new Error('Dom_b handles this')); 
});
   
dom_a.remove(emit_a);
emit_a.emit('error', new Error('Exception message...!'));

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.