• Node.js Video Tutorials

Node.js - os.hostname() 方法



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()方法将返回当前操作系统的 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()方法将当前操作系统的 hostname 打印到控制台。

The Hostname of this current computer is: Nikhilesh
nodejs_os_module.htm
广告

© . All rights reserved.