Node.js 的 os 模块提供了一堆与操作系统相关的实用方法和属性。
Node.js os.arch() 方法将返回编译当前Node.js的计算机的 CPU 架构。
有很多 CPU 架构,如 'x32'、'x64'、'arm'、'arm64'、's390'、's390x'、'mipsel'、'ia32'、'mips'、'ppc' 和 'ppc64'。要了解我们当前计算机的 CPU 架构,我们需要编译 os.arch() 方法。
语法
以下是 Node.js os.arch() 方法的语法 -
os.arch()
参数
此方法不接受任何参数。
返回值
此方法返回为其编译当前 node.js 二进制文件的操作系统 CPU 体系结构。
OS(操作系统)CPU架构的可能值为−
- 'arm'
- 'arm64'
- 'ia32'
- 'mips'
- 'mipsel'
- 'ppc'
- 'ppc64'
- 's390'
- 's390x'
- 'x64'
在下面的程序中,我们尝试使用 os.arch() 方法打印当前系统的 CPU 架构。
const os = require('os');
const {arch} = os;
console.log(os.arch());
输出
如果我们编译并运行上述程序,os.arch() 方法返回 'x64' 作为输出,因为该方法返回了当前计算机的 CPU 架构。
x64
例
在下面的示例中,
- 我们正在执行一个开关情况,以获取计算机的 CPU 架构。
- 因此,开关将根据 os.arch() 方法的输出字符串值检查每种情况,直到找到匹配项。
- 如果没有匹配项,将打印默认条件。
const os = require('os');
let arch = os.arch();
switch(arch) {
case 'arm':
console.log("This is a 32-bit 'Advanced RISC Machine'.");
break;
case 'arm64':
console.log("This is a 64-bit 'Advanced RISC Machine'.");
break;
case 'ia32':
console.log("This is a 32-bit 'Intel Architecture'.");
break;
case 'mips':
console.log("This is a 32-bit Microprocessor without Interlocked Pipelined Stages");
break;
case 'mipsel':
console.log("This is a 64-bit Microprocessor without Interlocked Pipelined Stages");
break;
case 'ppc':
console.log("This is a PowerPC Architecture.");
break;
case 'ppc64':
console.log("This is a 64-bit PowerPC Architecture.");
break;
case 's390':
console.log("This is a 31-bit IBM System/390, the third generation of the System/360 instruction set architecture");
break;
case 's390x':
console.log("This is a 64-bit IBM System/390, the third generation of the System/360 instruction set architecture.");
break;
case 'x64':
console.log("This is a 64-bit extended system.");
break;
case 'x32':
console.log("This is a 32-bit extended system.");
break;
}
输出
当我们编译并运行上述程序时,os.arch() 方法的输出字符串值将为 'x64'。因此,情况“x64”被匹配并执行。
This is a 64-bit extended system.