- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
Node.js - os.networkInterfaces() Method
The os module of Node.js provides a bunch of operating system-related utility methods and properties.
The Node.js os.networkInterfaces() method will return an object which contains the network interfaces that have been assigned a network address. In the returned object, each key specifies the network interface.
The returned object contains network interfaces with some properties. These properties are described below −
address − This is a string that has the assigned IPV4 or IPV6 address.
netmask − This is a string that has the assigned IPV4 or IPV6 network mask.
netmask − This string can be either IPV4 or IPV6.
mac − A string that shows the mac address of the network interface
internal − This is a Boolean value that will be true if the network interface is a loopback or similar interface that is not remotely accessible. Else, it is false.
scopeid − This is a number that has the IPV6 scope ID and is applicable only when the family is IPV6.
cidr − This is a string that specifies the assigned IPV4 or IPV6 address with the routing prefix in CIDR notation. The property is set to NULL if the netmask is invalid.
Syntax
Following is the syntax of the Node.js os.networkInterfaces() method −
os.networkInterfaces()
Parameters
This method does not accept any parameters.
Return value
This method returns an object containing information about network interfaces of a particular network.
Example
In the following example, we are trying to print the network interfaces of a network by using the os.networkInterfaces() method.
const os = require('os'); console.log("The network interfaces which have been assigned to this Network address are: ") console.log(os.networkInterfaces());
Output
As we can see in the output below, the current computer is connected to an Ethernet network, and the os.networkInterfaces() method returned the network interfaces of the connected 'Ethernet' network.
The network interfaces which have been assigned to this Network address are: { Ethernet: [ { address: 'fe80::46d1:d65a:b6cc:2926', netmask: 'ffff:ffff:ffff:ffff::', family: 'IPv6', mac: '88:a4:c2:bb:d1:f9', internal: false, cidr: 'fe80::46d1:d65a:b6cc:2926/64', scopeid: 8 }, { address: '192.168.2.33', netmask: '255.255.255.0', family: 'IPv4', mac: '88:a4:c2:bb:d1:f9', internal: false, cidr: '192.168.2.33/24' } ], 'Loopback Pseudo-Interface 1': [ { address: '::1', netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', family: 'IPv6', mac: '00:00:00:00:00:00', internal: true, cidr: '::1/128', scopeid: 0 }, { address: '127.0.0.1', netmask: '255.0.0.0', family: 'IPv4', mac: '00:00:00:00:00:00', internal: true, cidr: '127.0.0.1/8' } ] }
Example
In the following example, we are trying to print the details of various network interfaces to which the current system is connected.
const os = require('os'); console.log(os.networkInterfaces());
Output
After executing the above program, the os.networkInterfaces() method returned the network interfaces of the connected 'Wi-Fi' network.
{ 'Wi-Fi': [ { address: 'fe80::57e9:cc91:95fc:5f5b', netmask: 'ffff:ffff:ffff:ffff::', family: 'IPv6', mac: 'bc:f1:71:3b:eb:a1', internal: false, cidr: 'fe80::57e9:cc91:95fc:5f5b/64', scopeid: 16 }, { address: '192.168.25.229', netmask: '255.255.255.0', family: 'IPv4', mac: 'bc:f1:71:3b:eb:a1', internal: false, cidr: '192.168.25.229/24' } ], 'Loopback Pseudo-Interface 1': [ { address: '::1', netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', family: 'IPv6', mac: '00:00:00:00:00:00', internal: true, cidr: '::1/128', scopeid: 0 }, { address: '127.0.0.1', netmask: '255.0.0.0', family: 'IPv4', mac: '00:00:00:00:00:00', internal: true, cidr: '127.0.0.1/8' } ] }