📜  Node.js process.seteuid() 方法

📅  最后修改于: 2022-05-13 01:56:51.413000             🧑  作者: Mango

Node.js process.seteuid() 方法

process.seteuid() 方法是 process 模块的内置应用程序编程接口,用于设置 Node.js 进程的有效用户身份。

句法:

process.seteuid( id )

参数:此方法接受上面提到的单个参数,如下所述:

  • id:它是一个必需的参数,它包含一个字符串或一个整数,表示数字 ID 或用户名字符串。如果传递了用户名,它会在解析关联的数字 ID 时阻塞。

返回值:不返回任何值。

注意:此函数仅适用于 POSIX 平台。它在 windows 或 android 平台上不可用,因此会导致错误,即 TypeError, seteuid is not a 函数。

下面的例子说明了在 Node.js 中process.seteuid() 方法的使用:

示例 1:

Javascript
// 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());
}


Javascript
// 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) {
      
  // Within 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());
}


输出:

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

示例 2:

Javascript

// 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) {
      
  // Within 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