1. 程式人生 > 實用技巧 >nodejs.cn-Node.js-入門教程:Node.js 作業系統模組

nodejs.cn-Node.js-入門教程:Node.js 作業系統模組

ylbtech-nodejs.cn-Node.js-入門教程:Node.js 作業系統模組

1.返回頂部
1、

Node.js 作業系統模組

目錄

該模組提供了許多函式,可用於從底層的作業系統和程式執行所在的計算機上檢索資訊並與其進行互動。

const os = require('os')

有一些有用的屬性可以告訴我們一些與處理檔案有關的關鍵事項:

os.EOL可給出行定界符序列。 在 Linux 和 macOS 上為\n,在 Windows 上為\r\n

os.constants.signals可告知所有與處理過程訊號相關的常量,例如 SIGHUP、SIGKILL 等。

os.constants.errno可設定用於錯誤報告的常量,例如 EADDRINUSE、EOVERFLOW 等。

可以在http://nodejs.cn/api/os.html#os_signal_constants上閱讀所有的內容。

現在看一下os提供的主要方法:

os.arch()

返回標識底層架構的字串,例如armx64arm64

os.cpus()

返回關於系統上可用的 CPU 的資訊。

例如:

[
  {
    model: 'Intel(R) Core(TM)2 Duo CPU     P8600  @ 2.40GHz',
    speed: 2400,
    times: {
      user: 281685380,
      nice: 0,
      sys: 187986530,
      idle: 685833750,
      irq: 0
    }
  },
  {
    model: 'Intel(R) Core(TM)2 Duo CPU     P8600  @ 2.40GHz',
    speed: 2400,
    times: {
      user: 282348700,
      nice: 0,
      sys: 161800480,
      idle: 703509470,
      irq: 0
    }
  }
]

os.endianness()

根據是使用大端序或小端序編譯 Node.js,返回BELE

os.freemem()

返回代表系統中可用記憶體的位元組數。

os.homedir()

返回到當前使用者的主目錄的路徑。

例如:

'/Users/joe'

os.hostname()

返回主機名。

os.loadavg()

返回作業系統對平均負載的計算。

這僅在 Linux 和 macOS 上返回有意義的值。

例如:

[3.68798828125, 4.00244140625, 11.1181640625]

os.networkInterfaces()

返回系統上可用的網路介面的詳細資訊。

例如:

{ lo0:
   [ { address: '127.0.0.1',
       netmask: '255.0.0.0',
       family: 'IPv4',
       mac: 'fe:82:00:00:00:00',
       internal: true },
     { address: '::1',
       netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
       family: 'IPv6',
       mac: 'fe:82:00:00:00:00',
       scopeid: 0,
       internal: true },
     { address: 'fe80::1',
       netmask: 'ffff:ffff:ffff:ffff::',
       family: 'IPv6',
       mac: 'fe:82:00:00:00:00',
       scopeid: 1,
       internal: true } ],
  en1:
   [ { address: 'fe82::9b:8282:d7e6:496e',
       netmask: 'ffff:ffff:ffff:ffff::',
       family: 'IPv6',
       mac: '06:00:00:02:0e:00',
       scopeid: 5,
       internal: false },
     { address: '192.168.1.38',
       netmask: '255.255.255.0',
       family: 'IPv4',
       mac: '06:00:00:02:0e:00',
       internal: false } ],
  utun0:
   [ { address: 'fe80::2513:72bc:f405:61d0',
       netmask: 'ffff:ffff:ffff:ffff::',
       family: 'IPv6',
       mac: 'fe:80:00:20:00:00',
       scopeid: 8,
       internal: false } ] }

os.platform()

返回為 Node.js 編譯的平臺:

  • darwin
  • freebsd
  • linux
  • openbsd
  • win32
  • ...等

os.release()

返回標識作業系統版本號的字串。

os.tmpdir()

返回指定的臨時資料夾的路徑。

os.totalmem()

返回表示系統中可用的總記憶體的位元組數。

os.type()

標識作業系統:

  • Linux
  • macOS 上為Darwin
  • Windows 上為Windows_NT

os.uptime()

返回自上次重新啟動以來計算機持續執行的秒數。

os.userInfo()

2、
2.返回頂部
3.返回頂部
4.返回頂部
5.返回頂部
1、 http://nodejs.cn/learn/the-nodejs-os-module 2、
6.返回頂部
作者:ylbtech
出處:http://ylbtech.cnblogs.com/
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。