該模塊提供了許多函數(shù),可用于從底層的操作系統(tǒng)和程序運(yùn)行所在的計(jì)算機(jī)上檢索信息并與其進(jìn)行交互燃异。
const os = require('os')
有一些有用的屬性可以告訴我們一些與處理文件有關(guān)的關(guān)鍵事項(xiàng):
os.EOL
可給出行定界符序列吴裤。 在 Linux 和 macOS 上為 \n
,在 Windows 上為 \r\n
日裙。
os.constants.signals
可告知所有與處理過程信號(hào)相關(guān)的常量吹艇,例如 SIGHUP、SIGKILL 等昂拂。
os.constants.errno
可設(shè)置用于錯(cuò)誤報(bào)告的常量受神,例如 EADDRINUSE、EOVERFLOW 等格侯。
可以在 http://nodejs.cn/api/os.html#os_signal_constants 上閱讀所有的內(nèi)容鼻听。
現(xiàn)在看一下 os
提供的主要方法:
os.arch()
返回關(guān)于系統(tǒng)上可用的 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()
根據(jù)是使用大端序或小端序編譯 Node.js联四,返回 BE
或 LE
撑碴。
os.freemem()
返回代表系統(tǒng)中可用內(nèi)存的字節(jié)數(shù)。
os.homedir()
返回到當(dāng)前用戶的主目錄的路徑朝墩。
例如:
'/Users/joe'
os.hostname()
返回主機(jī)名醉拓。
os.loadavg()
返回操作系統(tǒng)對(duì)平均負(fù)載的計(jì)算。
這僅在 Linux 和 macOS 上返回有意義的值。
例如:
[3.68798828125, 4.00244140625, 11.1181640625]
os.networkInterfaces()
返回系統(tǒng)上可用的網(wǎng)絡(luò)接口的詳細(xì)信息亿卤。
例如:
{ 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 編譯的平臺(tái):
darwin
freebsd
linux
openbsd
win32
...等
os.release()
返回標(biāo)識(shí)操作系統(tǒng)版本號(hào)的字符串愤兵。
os.tmpdir()
返回指定的臨時(shí)文件夾的路徑。
os.totalmem()
返回表示系統(tǒng)中可用的總內(nèi)存的字節(jié)數(shù)排吴。
os.type()
標(biāo)識(shí)操作系統(tǒng):
- Linux
- macOS 上為Darwin
- Windows 上為 Windows_NT
os.uptime()
返回自上次重新啟動(dòng)以來計(jì)算機(jī)持續(xù)運(yùn)行的秒數(shù)秆乳。
os.userInfo()
文章來源 node中文官方 http://nodejs.cn/
更多知識(shí)點(diǎn) 請(qǐng)關(guān)注:筆墨是小舟