process.setuid()方法是流程模块的内置应用程序编程接口,用于设置Node.js流程的用户身份。
用法:
process.setuid(id)
参数:此方法接受上述和以下描述的单个参数:
- id:它是一个必需参数,其中包含一个String或一个整数,表示数字ID或用户名字符串,如果传递了用户名,则在解析关联的数字ID时会阻塞。
 
返回:它不返回任何值。
注意:此函数仅在POSIX平台上有效。在Windows或Android平台上不可用,因此会导致错误,即TypeError,setuid不是函数。
以下示例说明了Node.js中process.setuid()方法的使用:
范例1:
// Node.js program to demonstrate the      
// process.setuid() Method 
   
// Including process module 
const process = require('process'); 
  
// Print the user identity of the Node.js 
//  process and check whether the methods 
// exists or not 
if (process.getuid && process.setuid ) { 
    
  // Setting user id 
  process.setuid(400); 
    
  // Printing getuid() value 
  console.log("The user identity of the Node.js"
      + " process:", process.getuid()); 
}输出:
The user identity of the Node.js process:400
范例2:
// Node.js program to demonstrate the      
// process.setuid() Method 
   
// Including process module 
const process = require('process'); 
  
// Sett user id and check whether 
// the method exists or not 
if (process.setuid) { 
    
  // Withinn try catch 
  try { 
    process.setuid(696); 
    console.log("User id has successfully been set"); 
  } catch (err) { 
    console.log("Failed to set user id:", err); 
  } 
} 
  
// Check whether the method exists or not 
if (process.getuid) { 
    
  // Printing getuid() value 
  console.log("The user identity of the Node.js"
        + " process:", process.getuid()); 
}输出:
User id has successfully been set The user identity of the Node.js process:696
注意:上面的程序将通过使用node filename.js命令。
参考: https://nodejs.org/api/process.html#process_process_setuid_id
相关用法
- Node.js GM blur()用法及代码示例
 - Node.js GM drawRectangle()用法及代码示例
 - Node.js GM sharpen()用法及代码示例
 - Node.js GM drawPolyline()用法及代码示例
 - Node.js GM drawEllipse()用法及代码示例
 - Node.js GM drawCircle()用法及代码示例
 - Node.js GM drawPolygon()用法及代码示例
 
注:本文由纯净天空筛选整理自gekcho大神的英文原创作品 Node.js | process.setuid() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
