node.js 的 os 模块

时间:2022-09-17 04:55:20

  Node.js的os module 提供了一系列跟操作系统相关的操作函数,比较简单,所以功能也就十分有限。我们可以去官网看各个函数的介绍:

  http://nodejs.org/api/os.html

 var os = require('os');

 console.log('the os tmpdir is: ' + os.tmpdir());
console.log('the endianness of this os is: ' + os.endianness());
console.log('the hostname of this os is: ' + os.hostname());
console.log('the type of this os is: ' + os.type());
console.log('the platform of this os is: ' + os.platform());
console.log('the arch of this os is: ' + os.arch());
console.log('the release of the os is: ' + os.release());
console.log('the uptime os the os is: ' + os.uptime()); console.log('the end of line of this os is: ' + os.EOL); //os.EOL:操作系统的换行符
console.log('................................................');
console.log('................................................');
showObj(os.cpus());
console.log('................................................');
console.log('................................................');
showObj(os.networkInterfaces());
console.log('................................................');
console.log('................................................');
showObj(os.loadavg()); function showObj(obj){
if (obj == null) {
console.log('error: ' + obj);
return false;
}
for (var key in obj) {
//key is a string, so we cannot use obj.key to replace obj[key] at here. For example, if name is a property of obj:
      //(obj.name is right, but obj."name" is wrong.)
      //(obj["name"] is right, but obj[name] is wrong.)
if (typeof(obj[key]) == 'array' || typeof(obj[key]) == 'object') {
showObj(obj[key]);
} else {
if (obj[key] != null)
console.log(key + "=" + obj[key]);
}
}
}

参考博客地址:http://blog.csdn.net/simpleiseasy/article/details/7253429