这个fsPromises.chown()方法用于hange文件的所有权,然后解析承诺没有成功的争论。该函数接受可用于设置各自所有者和组的用户ID和组ID。
用法:
fsPromises.chown( path, uid, gid)
参数:此方法接受上述和以下所述的三个参数:
- path:它是一个字符串,缓冲区或URL,表示必须更改所有者和组的文件的路径。
 - uid:这是一个整数,表示与要设置的所有者相对应的用户ID。
 - gid:这是一个整数,表示与要设置的组相对应的组ID。
 
Javascript
// Node.js program to demonstrate the  
// fsPromises.chown() method  
  
// Import the filesystem module  
const fs = require('fs');  
  
let filepath = "example_file.txt";  
  
// Set the owner to a new one keeping the group same  
// New owner is "GeeksforGeeks" with user id 4567 
fsPromises.chown(filepath, 4567, 999 => {  
    console.log("uid and gid set successfully.");  
}); 在运行代码之前:
xubuntu@xubuntu:~/Desktop/fs-chown$ ls -l total 8 -rw-rw--w- 1 xubuntu xubuntu 4 May 26 04:08 example_file.txt -rw-rw-r-- 1 xubuntu xubuntu 290 May 26 04:08 index.js
代码输出:
Given uid and gid set successfully .
运行代码后:
xubuntu@xubuntu:~/Desktop/fs-chown$ ls -l total 8 -rw-rw--w- 1 geeksforgeeks xubuntu 4 May 26 04:08 example_file.txt -rw-rw-r-- 1 xubuntu xubuntu 290 May 26 04:08 index.js
范例2:本示例显示了组的设置。
Javascript
// Node.js program to demonstrate the  
// fsPromises.chown() method  
  
// Import the filesystem module  
const fs = require('fs');  
  
let filepath = "example_file.txt";  
  
// Set the owner and group both to a new one  
// New owner is "nitin" with owner id 8900  
// New group is "author" with group id 1024  
fsPromises.chown(filepath, 8900, 1024 => {  
  
console.log("uid and gid set successfully!");  
  
}); 在运行代码之前:
xubuntu@xubuntu:~/Desktop/fs-chown$ ls -l total 8 -rw-rw--w- 1 xubuntu xubuntu 4 May 26 04:19 example_file.txt -rw-rw-r-- 1 xubuntu xubuntu 290 May 26 04:19 index.js
代码输出:
Given uid and gid set successfully!
运行代码后:
xubuntu@xubuntu:~/Desktop/fs-chown$ ls -l total 8 -rw-rw--w- 1 nitin author 4 May 26 04:19 example_file.txt -rw-rw-r-- 1 xubuntu xubuntu 290 May 26 04:19 index.js
相关用法
- Node.js console.timeLog()用法及代码示例
 - Node.js fs.fsyncSync()用法及代码示例
 - Node.js process.nextTick()用法及代码示例
 - Node.js GM charcoal()用法及代码示例
 - Node.js GM blur()用法及代码示例
 - Node.js GM sharpen()用法及代码示例
 - Node.js GM drawLine()用法及代码示例
 - Node.js GM drawArc()用法及代码示例
 - Node.js GM drawPolyline()用法及代码示例
 - Node.js GM drawBezier()用法及代码示例
 - Node.js GM drawCircle()用法及代码示例
 
注:本文由纯净天空筛选整理自nitin_sharma大神的英文原创作品 Node.js | fsPromises.chown() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
