Node.js OS 模組
Node.js os 模組提供了一些基本的系統操作函數。我們可以通過以下方式引入該模組:
var os = require("os")
方法
序號 | 方法 & 描述 |
---|---|
1 | os.tmpdir() 返回操作系統的默認臨時檔夾。 |
2 | os.endianness() 返回 CPU 的位元組序,可能的是 "BE" 或 "LE"。 |
3 | os.hostname() 返回操作系統的主機名。 |
4 | os.type() 返回操作系統名 |
5 | os.platform() 返回編譯時的操作系統名 |
6 | os.arch() 返回操作系統 CPU 架構,可能的值有 "x64"、"arm" 和 "ia32"。 |
7 | os.release() 返回操作系統的發行版本。 |
8 | os.uptime() 返回操作系統運行的時間,以秒為單位。 |
9 | os.loadavg() 返回一個包含 1、5、15 分鐘平均負載的數組。 |
10 | os.totalmem() 返回系統記憶體總量,單位為位元組。 |
11 | os.freemem() 返回操作系統空閒記憶體量,單位是位元組。 |
12 | os.cpus() 返回一個對象數組,包含所安裝的每個 CPU/內核的資訊:型號、速度(單位 MHz)、時間(一個包含 user、nice、sys、idle 和 irq 所使用 CPU/內核毫秒數的對象)。 |
13 | os.networkInterfaces() 獲得網路介面列表。 |
屬性
序號 | 屬性 & 描述 |
---|---|
1 | os.EOL 定義了操作系統的行尾符的常量。 |
實例
創建 main.js 檔,代碼如下所示:
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.");
代碼執行結果如下:
$ node main.js endianness : LE type : Linux platform : linux total memory : 25103400960 bytes. free memory : 20676710400 bytes.