Related Articles

Related Articles

Node.js | os.hostname() Method
  • Last Updated : 14 Feb, 2020

The os.hostname() method is an inbuilt application programming interface of the os module which is used to get host name of the operating system.

Syntax:

os.hostname()

Parameters: This method does not accept any parameters.

Return Value: This method returns a string value that specifies the host name of the operating system.

Below examples illustrate the use of os.hostname() method in Node.js:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the    
// os.hostname() method 
     
// Allocating os module
const os = require('os');
  
// Printing os.hostname() value
console.log(os.hostname());

chevron_right


Output:

gekchos_lappy

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the    
// os.hostname() method 
     
// Allocating os module
const os = require('os');
  
// Printing os.hostname() value
if(os.hostname()) {
    var hostname = os.hostname();
    console.log("Hostname for the operating"
        + " system is " + String(hostname));
}

chevron_right


Output:

Hostname for the operating system is gekchos_lappy

Note: The above program will compile and run by using the node index.js command.

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :