当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js fsPromises.chmod()用法及代码示例


fsPromises.chmod()方法用于更改给定路径的权限。可以使用字符串常量或与它们各自的文件模式相对应的八进制数字来指定这些权限。

注意:Windows平台仅支持更改写权限。它不支持区分用户权限,组权限或其他权限。它更改文件的权限,然后在成功时不带任何参数地解决Promise。

用法:

fsPromises.chmod( path, mode)

参数:该方法接受上述和以下所述的两个参数:

  • path:它是一个字符串,Buffer或URL,表示必须更改其权限的文​​件的路径。
  • mode:它是字符串或八进制整数常量,表示要授予的权限。逻辑或运算符可用于分隔多个权限。

以下示例说明了Node.js中的fsPromises.chmod()方法:



范例1:本示例说明使用八进制整数常量来授予文件权限。

// Node.js program to demonstrate the  
// fsPromises.chmod() method  
     
// Import the filesystem module  
const fs = require('fs');  
const fsPromises = require('fs').promises; 
  
console.log("Granting only read access to user");  
     
fsPromises.chmod("example.txt", 0o400 )  
  
console.log("\nReading the file contents");  
  
console.log(fs.readFileSync("example.txt", 'utf8'));  
     
console.log("\nTrying to write to file");  
    
try {  
    fs.writeFileSync('example.txt',  
    "This file has now been edited.");  
}  
catch (e) {  
    console.log("Error Code:", e.code);  
}  
     
console.log("\nGranting read and write access to user");  
  
fsPromises.chmod("example.txt", 0o600 )  
  
console.log("Trying to write to file");  
  
fs.writeFileSync('example.txt',  
    "This file has now been edited.");  
    
console.log("\nReading the file contents");  
console.log(fs.readFileSync("example.txt", 'utf8')); 

输出:

Granting only read access to user

Reading the file contents     
This file has now been edited.

Trying to write to file       
Error Code:EPERM

Granting read and write access to user
Trying to write to file

Reading the file contents
This file has now been edited.

范例2:本示例说明了使用字符串常量和或运算符赋予文件权限的方法

// Node.js program to demonstrate the  
// fsPromises.chmod() method  
    
// Import the filesystem module  
const fs = require('fs');  
const fsPromises = fs.promises; 
    
// Grant only read permission to user  
console.log("Granting only read access to user");  
fsPromises.chmod("example.txt", fs.constants.R_OK); 
    
// Reading the file  
console.log("File Contents:",  
    fs.readFileSync("example.txt", 'utf8'));  
    
// Trying to write to file  
try {  
  console.log("\nTrying to write to file");  
  fs.writeFileSync('example.txt',  
    "This file now has been edited.");  
}  
catch (e) {  
  console.log( 
   "Error Occurred, Error Code:", e.code);  
}  
    
// Granting both read and  
// write permission  
console.log("\nGranting both read and "
        + 'write permission to user");  
fsPromises.chmod("example.txt",  
   fs.constants.R_OK | fs.constants.W_OK); 
  
// Check the file mode  
console.log("Current File Mode:",  
    fs.statSync("example.txt").mode);  
  
console.log("Trying to write to file");  
try 
{ 
  fs.writeFileSync('example.txt',  
    "This file now has been edited.");  
} catch (e) { 
  console.log("Error ", e.code); 
} 
  
console.log("File Contents:",  
    fs.readFileSync("example.txt", 'utf8')); 

输出:

Granting only read access to user
File Contents:This file has now been edited.  

Trying to write to file
Error Occurred, Error Code:EPERM

Granting both read and write permission to user
Current File Mode:33060
Trying to write to file
Error  EPERM
File Contents:This file has now been edited.

参考: https://nodejs.org/api/fs.html#fs_fspromises_chmod_path_mode




相关用法


注:本文由纯净天空筛选整理自nitin_sharma大神的英文原创作品 Node.js | fsPromises.chmod() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。