Node.js 的 os 模块提供了一堆与操作系统相关的实用方法和属性。
Node.js os.hostame() 方法将获取当前操作系统的主机名。这用于检索操作系统的网络名称。此方法将输出作为字符串返回。返回的值也可能有所不同,具体取决于操作系统的设置和配置方式。
语法
以下是Node.js os.hostname()方法的语法 -
os.hostname()
参数
此方法不接受任何参数。
返回值
此方法将以字符串形式返回当前操作系统的主机名称。
例在以下示例中,我们最初导入了 os 模块。然后我们尝试使用 os.hostname() 方法打印当前操作系统的主机名。
const os = require('os');
console.log(os.hostname());
输出
82a25c5e95a1
注意 - 为了获得准确的结果,最好在本地执行上述代码。
如果我们编译并运行上述程序,os.hostname() 方法将返回当前操作系统的主机名。
Nikhilesh
例
以下是获取当前操作系统主机名的另一个示例。
const os = require('os');
var name_of_the_host = os.hostname();
do{
console.log('The Hostname of this current computer is: ' + name_of_the_host);
break;
}
while (os.hostname());
输出
The Hostname of this current computer is: 82a25c5e95a1
注意 - 为了获得准确的结果,最好在本地执行上述代码。
执行上述程序后,os.hostname() 方法将当前操作系统的主机名打印到控制台。
The Hostname of this current computer is: Nikhilesh