• Node.js Video Tutorials

Node.js - os.arch() 方法



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

示例

在下面的示例中,

  • 我們正在執行 switch case 語句來獲取計算機的 CPU 架構。

  • 因此,switch 語句將檢查每個 case 與 os.arch() 方法 的輸出字串值是否匹配,直到找到匹配項。

  • 如果沒有任何匹配項,則將列印 default 條件。

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'。因此,case 'x64' 匹配並執行。

This is a 64-bit extended system.
nodejs_os_module.htm
廣告