Node.js核心模块-os操作系统
获取操作系统相关信息
引用
const os = require('os')
os.cpus()
获取当前机器的CPU信息
console.log(os.cpus())
打印结果:
[ { model: 'Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz', speed: 2300, times: { user: 1332960, nice: 0, sys: 928720, idle: 5905010, irq: 0 } }, { model: 'Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz', speed: 2300, times: { user: 684730, nice: 0, sys: 490170, idle: 6991300, irq: 0 } }, { model: 'Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz', speed: 2300, times: { user: 1315900, nice: 0, sys: 741750, idle: 6108540, irq: 0 } }, { model: 'Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz', speed: 2300, times: { user: 596770, nice: 0, sys: 406270, idle: 7163160, irq: 0 } } ]
os.totalmem()
获取当前机器的内存总量,单位为字节
console.log(os.totalmem())
打印结果:
17179869184
单位为字节,除以1024为KB,再除以1024为MB,再除以1024为GB(上面的结果为16GB)
os.release()
以字符串的形式返回操作系统
const os = require('os') console.log(os.release()) //19.6.0
os.homedir()
返回当前用户的主目录的字符串路径
const os = require('os') console.log(os.homedir()) // /Users/lianglanlan
os.hostname()
返回操作系统主机名,字符串格式
const os = require('os') console.log('os获取的hostname是', os.hostname()) //os获取的hostname是 LANLANLIANG-MB0