Node.js process.platform 属性
process.platform 属性是进程模块的内置应用程序编程接口,用于获取操作系统平台信息。
句法:
process.platform
返回值:此属性返回一个表示操作系统平台的字符串。返回值可以是“aix”、“android”、“darwin”、“freebsd”、“linux”、“openbsd”、“sunprocess”和“win32”之一。此值在编译时设置。
下面的例子说明了在 Node.js 中process.platform 属性的使用:
示例 1:
Javascript
// Node.js program to demonstrate the
// process.platform Property
// Include process module
const process = require('process');
// Printing process.platform property value
console.log(process.platform);
Javascript
// Node.js program to demonstrate the
// process.platform Property
// Include process module
const process = require('process');
// Printing process.platform property value
var platform = process.platform;
switch(platform) {
case 'aix':
console.log("IBM AIX platform");
break;
case 'darwin':
console.log("Darwin platform(MacOS, IOS etc)");
break;
case 'freebsd':
console.log("FreeBSD Platform");
break;
case 'linux':
console.log("Linux Platform");
break;
case 'openbsd':
console.log("OpenBSD platform");
break;
case 'sunos':
console.log("SunOS platform");
break;
case 'win32':
console.log("windows platform");
break;
default:
console.log("unknown platform");
}
输出:
win32
示例 2:
Javascript
// Node.js program to demonstrate the
// process.platform Property
// Include process module
const process = require('process');
// Printing process.platform property value
var platform = process.platform;
switch(platform) {
case 'aix':
console.log("IBM AIX platform");
break;
case 'darwin':
console.log("Darwin platform(MacOS, IOS etc)");
break;
case 'freebsd':
console.log("FreeBSD Platform");
break;
case 'linux':
console.log("Linux Platform");
break;
case 'openbsd':
console.log("OpenBSD platform");
break;
case 'sunos':
console.log("SunOS platform");
break;
case 'win32':
console.log("windows platform");
break;
default:
console.log("unknown platform");
}
输出:
windows platform
注意:上面的程序将使用 node filename.js 命令编译和运行。
参考: https://nodejs.org/api/process.html#process_process_platform