4.安装cpu
npm i cpu-stat -S
5.模块
总共分为三种:
1.内建模块
const os = require(‘os‘);
const mem = os.freemem() / os.totalmem() *100
console.log(`内存占用率:${mem}%`)
2.第三方模块 需要安装cpu-stat // npm i cpu-stat -S
const cpuStat = require(‘cpu-stat‘)
cpuStat.usagePercent((err,percent)=>{
console.log(`cpu占用率:${percent}`)
})
3.自定义模块
const conf = require(‘./conf‘);
console.log(conf);
conf.js 文件如下:
module.exports = {
dbname:‘test‘,
user:‘admin‘,
password:‘123‘
};
const {rmbToDollar} = require(‘./currency‘)(6);
console.log(rmbToDollar(100));
currency.js 文件如下:
let rate;
function rmbToDollar(rmb){
return rmb/rate;
}
module.exports= function(r){
rate = r;
return {rmbToDollar};
}