Site icon DataFlair

Nodejs Utility Module

nodejs utility module

FREE Online Courses: Click for Success, Learn for Free - Start Now!

In this article we will be discussing the Nodejs utility module in detail with code and examples.

Nodejs Utility Modules

1. OS Module in Nodejs

It is a built-in utility module in nodejs. To include it in our file we use the require() function. This module is used to get information about the operating system where the code is being executed.

Methods of OS Module:

Properties of OS Module:

Code for os module:

const os = require("os");
console.log('Operating System type : ' + os.type());
console.log('Operating System platform : ' + os.platform());
console.log('Operating System total memory : ' + os.totalmem() + " bytes.");

Output

2. Path module in Nodejs

This module is used to get the file paths. To include it in our file we use the require() function.

Methods of Path Module:

Properties of Path Modules:

Code for path modules:

const path = require('path');
console.log('resolve:' + path.resolve('paths.js'));
console.log('File extension:' + path.extname('paths.js'));

Output:

3. DNS module in Nodejs

This module gives us information regarding the network types.To include it in our file we use the require() function.

Methods of DNS Module:
Code for Dns module:
const dns = require('dns');
dns.lookup("www.data-flair.training", (err, address, family) => {
console.log('Address of %s is %j family: IPv%s', 'www.data-flair.training/', address, family);
});

Output:

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

4. Net module in Nodejs

It is used for the creation of servers and clients. To include it in our file we use the require() function.

Methods of net Module:
Code for net module:
const net = require('net');
const server = net.createServer(function (connection) {
console.log('client connection completed'); connection.on('end', function () {
console.log('client has been disconnected');
});
connection.write('Welcome to DataFlair\n'); connection.pipe(connection);
});

server.listen(3000, function () {
console.log('server listening');
});
const client = net.connect(3000, function () {
console.log('Client Connected');
client.write('DataFlair\r\n');
});
client.on('data', function (data) {
console.log(data.toString());
client.end();
});
client.on('end', function () {
console.log('Server Disconnected');
});

Output:

5. Domain Module in Nodejs

It is used to intercept the errors which are not handled. It can be handled using internal binding or external binding.

Methods of Domain Module:
Properties of Domain Module:
Code for domain Module:
let EventEmitter = require("events").EventEmitter;
let domain = require("domain");
let emitter1 = new EventEmitter();

let domain1 = domain.create();

domain1.on('error', function (err) {
console.log("domain1 handled this error (" + err.message + ")");
});
domain1.add(emitter1);

emitter1.on('error', function (err) {
console.log("event listener handled this error (" + err.message + ")");
});
emitter1.emit('error', new Error('It will be handled by listener'));
emitter1.removeAllListeners('error');
emitter1.emit('error', new Error('It will be handled by domain1'));
var domain2 = domain.create();
domain2.on('error', function (err) {
console.log("domain2 handled this error (" + err.message + ")");
});
domain2.run(function () {
let emitter2 = new EventEmitter();
emitter2.emit('error', new Error('It will be handled by domain2'));
});
domain1.remove(emitter1);
emitter1.emit('error', new Error('Converted to exception. Program will crash!'));

Output:

event listener handled this error (It will be handled by listener)
domain1 handled this error (It will be handled by domain1)
domain2 handled this error (It will be handled by domain2)
node:events:371
throw er; // Unhandled ‘error’ event
^Error: Converted to exception. Program will crash!
!

Util properties and Methods:

Summary:

In this article, we have thoroughly discussed the various utility modules in node js. For other related topics check the DataFlair website.

Exit mobile version