Monday, July 29, 2019

What is Node.js OS ?

Node.js OS provides some basic operating-system related utility functions. Let's see the list generally used functions or methods.
IndexMethodDescription
1.os.arch()This method is used to fetch the operating system CPU architecture.
2.
os.cpus()
This method is used to fetch an array of objects containing information about each cpu/core installed: model, speed (in MHz), and times (an object containing the number of milliseconds the cpu/core spent in: user, nice, sys, idle, and irq).
3.os.endianness()This method returns the endianness of the cpu. Its possible values are 'BE' for big endian or 'LE' for little endian.
4.os.freemem()This methods returns the amount of free system memory in bytes.
5.os.homedir()This method returns the home directory of the current user.
6.
os.hostname()
This method is used to returns the hostname of the operating system.
7.os.loadavg()This method returns an array containing the 1, 5, and 15 minute load averages. The load average is a time fraction taken by system activity, calculated by the operating system and expressed as a fractional number.
8.os.networkinterfaces()This method returns a list of network interfaces.
9.os.platform()This method returns the operating system platform of the running computer i.e.'darwin', 'win32','freebsd', 'linux', 'sunos' etc.
10.os.release()This method returns the operating system release.
11.os.tmpdir()This method returns the operating system's default directory for temporary files.
12.os.totalmem()This method returns the total amount of system memory in bytes.
13.os.type()This method returns the operating system name. For example 'linux' on linux, 'darwin' on os x and 'windows_nt' on windows.
14.os.uptime()This method returns the system uptime in seconds.
15.os.userinfo([options])This method returns a subset of the password file entry for the current effective user.

Node.js OS Example 1

In this example, we are including some basic functions. Create a file named os_example1.js having the following code:
File: os_example1.js
  1. const os=require('os');  
  2. console.log("os.freemem(): \n",os.freemem());  
  3. console.log("os.homedir(): \n",os.homedir());  
  4. console.log("os.hostname(): \n",os.hostname());  
  5. console.log("os.endianness(): \n",os.endianness());  
  6. console.log("os.loadavg(): \n",os.loadavg());  
  7. console.log("os.platform(): \n",os.platform());  
  8. console.log("os.release(): \n",os.release());  
  9. console.log("os.tmpdir(): \n",os.tmpdir());  
  10. console.log("os.totalmem(): \n",os.totalmem());  
  11. console.log("os.type(): \n",os.type());  
  12. console.log("os.uptime(): \n",os.uptime());  
Open Node.js command prompt and run the following code:
  1. node os_example1.js  
Node.js os example 1

Node.js OS Example 2

In this example, we are including remaining functions. Create a file named os_example2.js having the following code:
File: os_example2.js
  1. const os=require('os');  
  2. console.log("os.cpus(): \n",os.cpus());  
  3. console.log("os.arch(): \n",os.arch());  
  4. console.log("os.networkInterfaces(): \n",os.networkInterfaces());   
Open Node.js command prompt and run the following code:
  1. node os_example2.js  
Node.js os example 2

No comments:

Post a Comment