学习如何使用Node.js模块

100 阅读3分钟

开始使用Node.js模块

我们使用Node.js操作系统(OS)模块来获取更多关于底层计算机系统的信息。在本教程中,你将学习Node.js中的基本操作系统模块操作。

开始学习

自从ES2015发布以来,JavaScript已经改进了,采用了模块的使用。这一功能允许脚本在多个文件中重复使用。

鉴于这一优势,Node.js也不例外,它将大部分的核心功能组织在模块中。

本教程假定你有Node.js的基本知识和它的核心功能。

操作系统(在Node.js中)是一个模块,因此我们需要在访问其核心方法之前将其导入我们的脚本中。

让我们先创建一个文件node-os-module.js ,并添加以下一行。

const os = require('os');

现在我们已经有了操作系统模块,在下一节,让我们看看我们可以用操作系统模块进行的各种操作。

获取操作系统的详细信息

这个模块有几个函数,用来检索details of an operating system ,我们很快就会看到。

node-os-module.js 文件中,添加以下脚本。

const os = require('os');

let myCurrentOSDetails = {
    name: os.type(),
    architecture: os.arch(),
    platform: os.platform(),
    release: os.release(),
    version: os.version()
};

console.log(myCurrentOSDetails);

在你的终端中运行这个脚本,通过在你的shell中输入以下内容。

node node-os-module.js 

在Ubuntu 20.04中运行这个脚本,输出如下(注意,结果因你的系统而异)。

{
  name: 'Linux',
  architecture: 'x64',
  platform: 'linux',
  release: '5.8.0-45-generic',
  version: '#51~20.04.1-Ubuntu SMP Tue Feb 23 13:46:31 UTC 2021'
}

你可以通过使用Node.js的Read, Evaluate, Print and Loop (REPL) 命令行工具实现同样的结果。

首先在你的终端运行以下命令,启动REPL

$ node

运行node 命令后,你会发现屏幕上出现了> 符号,这就是REPL 命令行(如下图所示)。

$ node
Welcome to Node.js v15.12.0.
Type ".help" for more information.
> 

现在让我们继续,在REPL 中写下我们之前的脚本,如下图所示。

$ node
Welcome to Node.js v15.12.0.
Type ".help" for more information. 
> const os = require('os');
undefined
> 
> let myCurrentOSDetails = {
...     name: os.type(),
...     architecture: os.arch(),
...     platform: os.platform(),
...     release: os.release(),
...     version: os.version()
... };
undefined
>

由于我们的脚本被分配给了myCurrentOSDetails 变量,我们可以记录我们的结果,如下图所示。

> console.log( myCurrentOSDetails );
> {
  name: 'Linux',
  architecture: 'x64',
  platform: 'linux',
  release: '5.8.0-45-generic',
  version: '#51~20.04.1-Ubuntu SMP Tue Feb 23 13:46:31 UTC 2021'
}
undefined
>

检查计算机系统(服务器)的正常运行时间

我们已经看到了如何使用这个模块来获得服务器底层操作系统的详细信息,在这一节中,让我们看看server uptime 的功能。

计算机中的正常运行时间指的是系统执行其操作的可用性,而不像停机时间那样,系统停滞/关闭,无法执行其任务。

这个模块有os.uptime() 方法,可以返回系统正常运行时间的详细信息,单位是秒(从你的系统最后一次开机时间算起)。

让我们看一个例子。

在你的REPL 窗口,输入以下命令。

...
> console.log(`Server running for the past ${os.uptime()} seconds.`);

输出。

...
> console.log(`Server running for the past ${os.uptime()} seconds.`);
Server running for the past 750588.69 seconds.
undefined
> 

获取用户信息(当前系统用户)

操作系统模块有os.userinfo() 方法,可以返回当前系统用户的详细信息(记住计算机系统可能有一个root user 和一个normal user ,这取决于其配置)。

你可以通过运行下面的REPL 命令来实现这个任务(记得要像前面讨论的那样导入os模块)。

console.log(os.userInfo());

在Ubuntu 20.04中输出。

> console.log(os.userInfo());
{
  uid: 1000,
  gid: 1000,
  username: 'xxxmixxxer',
  homedir: '/home/xxxmixxxer',
  shell: '/bin/bash'
}
undefined
>

计算机系统硬件信息

OS模块有几种方法可以获得计算机系统的详细信息,我们主要关注的是硬件。

它们包括。

  • os.totalmem() - 该方法以字节为单位返回计算机的主要存储(内存)。

我们可以通过在REPL 中输入以下内容来实现。

 > let memory = os.totalmem();
undefined
> 
 > console.log(memory);
16628015104
undefined
> 

注意:返回的值是以字节为单位的,这可能需要额外的算术进行转换。即16628015104/1024=~16GB的内存。

  • os.freemem() - 该方法返回计算机的自由空间。可用空间的大小,以字节为单位。

比如说。

 > let freeMememorySpace = os.freemem();
undefined
> 
 > console.log(freeMememorySpace);
1298567168
undefined
> 

结论

我们已经看到我们如何使用os module ,以获得更多关于计算机系统的信息。这一点很重要,特别是当需要关于主机(也许是服务器)平台的知识时。