NodeJs-os模块

Node.js os 模块提供了一些基本的系统操作函数。我们可以通过以下方式引入该模块:

1
var os = require("os")

方法

序号

方法 & 描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
os.tmpdir() 返回操作系统的默认临时文件夹。

os.endianness() 返回 CPU 的字节序,可能的是 "BE" 或 "LE"。

os.hostname() 返回操作系统的主机名。

os.type() 返回操作系统名

os.platform() 返回编译时的操作系统名

os.arch() 返回操作系统 CPU 架构,可能的值有 "x64"、"arm" 和 "ia32"。

os.release() 返回操作系统的发行版本。

os.uptime() 返回操作系统运行的时间,以秒为单位。

os.loadavg() 返回一个包含 1、5、15 分钟平均负载的数组。

os.totalmem() 返回系统内存总量,单位为字节。

os.freemem() 返回操作系统空闲内存量,单位是字节。

os.cpus() 返回一个对象数组,包含所安装的每个 CPU/内核的信息:型号、速度(单位 MHz)、时间(一个包含 user、nice、sys、idle 和 irq 所使用 CPU/内核毫秒数的对象)。

os.networkInterfaces() 获得网络接口列表。

属性

序号

属性 & 描述

os.EOL 定义了操作系统的行尾符的常量。

实例

创建 main.js 文件,代码如下所示:

1
2
3
4
5
6
7
8
9
10
11
var os = require("os");

// CPU 的字节序 console.log('endianness : ' + os.endianness());

// 操作系统名 console.log('type : ' + os.type());

// 操作系统名 console.log('platform : ' + os.platform());

// 系统内存总量 console.log('total memory : ' + os.totalmem() + " bytes.");

// 操作系统空闲内存量 console.log('free memory : ' + os.freemem() + " bytes.");

代码执行结果如下:

1
2
3
4
$ node main.js 
endianness : LE
type : Linux platform : linux
total memory : 25103400960 bytes. free memory : 20676710400 bytes.
  • 版权声明: 本博客所有文章,未经许可,任何单位及个人不得做营利性使用!转载请标明出处!如有侵权请联系作者。
  • Copyrights © 2015-2023 翟天野

请我喝杯咖啡吧~