當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Node.js process.seteuid()用法及代碼示例


process.seteuid()方法是流程模塊的內置應用程序編程接口,用於設置Node.js流程的有效用戶身份。

用法:

process.seteuid( id )

參數:此方法接受上述和以下描述的單個參數:



  • id:它是一個必需參數,它包含一個String或一個整數,表示數字ID或用戶名字符串。如果傳遞了用戶名,則在解析關聯的數字ID時會阻塞。

返回值:它不返回任何值。

注意:此函數僅在POSIX平台上有效。它在Windows或Android平台上不可用,因此會導致錯誤,即TypeError,seteuid不是函數。

以下示例說明了Node.js中process.seteuid()方法的使用:

範例1:

// Node.js program to demonstrate the      
// process.seteuid() method 
   
// Include process module 
const process = require('process'); 
  
// Printing the effective user identity 
// of the Node.js process and checking 
// whether the method exists or not 
if (process.geteuid && process.seteuid ) { 
      
  // Setting user id 
  process.seteuid(400); 
    
  // Printing geteuid() 
  console.log("The effective user identity "
        + "of the Node.js process:"
        + process.geteuid()); 
}

輸出:

The effective user identity of the Node.js process:400

範例2:

// Node.js program to demonstrate the      
// process.seteuid() method 
   
// Include process module 
const process = require('process'); 
  
// Set user id and checking whether 
// the method exists or not 
if (process.seteuid) { 
      
  // Withinn try catch 
  try { 
      process.seteuid(696); 
      console.log("User id has successfully been set"); 
  } catch (err) { 
      console.log("Failed to set user id:", err); 
  } 
} 
  
// Checking whether the method exists or not 
if (process.geteuid) { 
  
    // Printing geteuid() value 
    console.log("The numerical effective user "
          + "identity of the Node.js process:"
          + process.geteuid()); 
}

輸出:

User id has successfully been set
The numerical effective user identity of the Node.js process:696

注意:上麵的程序將通過使用node filename.js命令。

參考: https://nodejs.org/api/process.html#process_process_seteuid_id




相關用法


注:本文由純淨天空篩選整理自gekcho大神的英文原創作品 Node.js | process.seteuid() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。