📜  Node.js操作系统

📅  最后修改于: 2020-12-24 03:32:02             🧑  作者: Mango

Node.js操作系统

Node.js OS提供了一些与操作系统相关的基本实用程序功能。让我们看一下常用的函数或方法列表。

Index Method Description
1. os.arch() This method is used to fetch the operating system CPU architecture.
2.
os.cpus()
This method is used to fetch an array of objects containing information about each cpu/core installed: model, speed (in MHz), and times (an object containing the number of milliseconds the cpu/core spent in: user, nice, sys, idle, and irq).
3. os.endianness() This method returns the endianness of the cpu. Its possible values are ‘BE’ for big endian or ‘LE’ for little endian.
4. os.freemem() This methods returns the amount of free system memory in bytes.
5. os.homedir() This method returns the home directory of the current user.
6.
os.hostname()
This method is used to returns the hostname of the operating system.
7. os.loadavg() This method returns an array containing the 1, 5, and 15 minute load averages. The load average is a time fraction taken by system activity, calculated by the operating system and expressed as a fractional number.
8. os.networkinterfaces() This method returns a list of network interfaces.
9. os.platform() This method returns the operating system platform of the running computer i.e.’darwin’, ‘win32′,’freebsd’, ‘linux’, ‘sunos’ etc.
10. os.release() This method returns the operating system release.
11. os.tmpdir() This method returns the operating system’s default directory for temporary files.
12. os.totalmem() This method returns the total amount of system memory in bytes.
13. os.type() This method returns the operating system name. For example ‘linux’ on linux, ‘darwin’ on os x and ‘windows_nt’ on windows.
14. os.uptime() This method returns the system uptime in seconds.
15. os.userinfo([options]) This method returns a subset of the password file entry for the current effective user.

Node.js OS示例1

在此示例中,我们包括一些基本功能。创建具有以下代码的名为os_example1.js的文件:

文件:os_example1.js

const os=require('os');
console.log("os.freemem(): \n",os.freemem());
console.log("os.homedir(): \n",os.homedir());
console.log("os.hostname(): \n",os.hostname());
console.log("os.endianness(): \n",os.endianness());
console.log("os.loadavg(): \n",os.loadavg());
console.log("os.platform(): \n",os.platform());
console.log("os.release(): \n",os.release());
console.log("os.tmpdir(): \n",os.tmpdir());
console.log("os.totalmem(): \n",os.totalmem());
console.log("os.type(): \n",os.type());
console.log("os.uptime(): \n",os.uptime());

打开Node.js命令提示符并运行以下代码:

node os_example1.js

Node.js OS示例2

在此示例中,我们包括其余功能。使用以下代码创建一个名为os_example2.js的文件:

文件:os_example2.js

const os=require('os');
console.log("os.cpus(): \n",os.cpus());
console.log("os.arch(): \n",os.arch());
console.log("os.networkInterfaces(): \n",os.networkInterfaces()); 

打开Node.js命令提示符并运行以下代码:

node os_example2.js