Monday, March 13, 2023
HomeNodejsNodeJS OS Module: A Full Information

NodeJS OS Module: A Full Information


NodeJS OS is used to get info relating to the OS put in on the machine through which Node is operating. Node OS will assist to work together with the Operation System operating the Node.js server to handle the assets, efficiency, and many others. Additionally it is to get info relating to the CPU, structure, operation system uptime, system’s constraints, complete reminiscence, free reminiscence accessible, hostname, and many others.

Suppose we’re operating an enormous Node.js server on a machine, and we now have to handle the server in order that it received’t take the entire reminiscence, if it takes full reminiscence, then there’s a excessive likelihood that the server will crash. So we are able to use the Node OS to get the details about the whole system reminiscence and the free reminiscence accessible and design the server in a manner that releases some unused reminiscence earlier than it’s full.

Not solely this, there are numerous functionalities supplied by the Node OS module, let’s see them one after the other.

Node OS Module Strategies

Earlier than going additional be sure to have Node put in in order that you’ll be able to run the given instructions and examples code in your system. Kind the beneath command within the terminal, if it returns any worth it means Node.js is efficiently put in.

arch()

This technique is used to get info relating to the system structure.

Instance:

const os = require('os');

const output = os.arch();
console.log(output);

Output:

cpus()

This technique returns the accessible CPUs on the system.

Instance:

const os = require('os');

const output = os.cpus()
console.log(output);

Output:

[
  {
    model: '11th Gen Intel(R) Core(TM) i3-1115G4 @ 3.00GHz',
    speed: 2995,
    times: {
      user: 13165093,
      nice: 0,
      sys: 13549453,
      idle: 144999625,
      irq: 1201968
    }
  },
  {
    model: '11th Gen Intel(R) Core(TM) i3-1115G4 @ 3.00GHz',
    speed: 2995,
    times: {
      user: 11404109,
      nice: 0,
      sys: 9191968,
      idle: 151117968,
      irq: 181578
    }
  }...

endianness()

This method returns the endianness of the CPU.

Example:

const os = require('os');

const output = os.endianness()
console.log(output);

Output:

freemem()

This method returns the free memory of the system.

Example:

const os = require('os');

const output = os.freemem()
console.log(output);

Output:

totalmem()

Returns the total memory of the system.

Example:

const os = require('os');

const output = os.totalmem()
console.log(output);

Output:

hostname()

This method gives the hostname of the system.

Example:

const os = require('os');

const output = os.hostname()
console.log(output);

Output:

userInfo()

This method gives information regarding the currently logged user.

Example:

const os = require('os');

output = os.userInfo()
console.log(output);

Output:

{
  uid: -1,
  gid: -1,
  username: 'ag290',
  homedir: 'C:Usersag290',
  shell: null
}

platform()

Returns the platform of the system.

Example:

const os = require('os');

const output = os.platform()
console.log(output);

Output:

release()

Returns the information regarding the release of the operating system.

Example:

const os = require('os');

const output = os.release()
console.log(output);

Output:

type()

Return the types of the operating system.

Example:

const os = require('os');

const output = os.type()
console.log(output);

Output:

networkInterfaces()

This method returns the network interface.

Example:

const os = require('os');

const output = os.networkInterfaces()
console.log(output);

Output:

uptime()

Returns the system uptime.

Example:

const os = require('os');

const output = os.uptime()
console.log(output);

Output:

Cloud System Specifications

We host our Node.js application on different types of cloud os, and the company charges us according to the uses and memory so we can also use this module to track the memory uses to decrease the cost, also you can double-check that the architect and CPUs, available memory are the same as mentioned in the plan or they are falsely claiming.

Summary

OS is a built-in module in Node.js used to interact with the operating system and gives different types of information like CPU architecture, currently logged users, total memory, free memory, and more. It is useful during the creation of a server on a system to know the details about the system for best performance. It also helps in memory management. Hope this article helps you to understand the Node OS.

Reference

https://nodejs.org/api/os.html#os

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments