fs.access()方法用于测试给定文件或目录的权限。可以使用文件访问常量将要检查的权限指定为参数。也可以通过使用按位或运算符创建具有多个文件常量的掩码来检查多个文件权限。
注意:不建议在调用fs.open(),fs.readFile()或fs.writeFile()之前使用fs.access()方法检查文件的可访问性,因为它会引入竞争状态,因为文件状态可能会在之后被其他进程更改考试。
用法:
fs.access( path, mode, callback )
参数:此方法接受上述和以下所述的三个参数:
- path:它是一个字符串,缓冲区或URL,表示必须对其权限进行测试的文件或目录的路径。
 - mode:它是一个整数值,表示要测试的许可。逻辑OR运算符可用于分隔多个权限。它可以具有值
fs.constants.F_OK,fs.constants.R_OK,fs.constants.W_OK和fs.constants.X_OK。它是一个可选参数。默认值为fs.constants.F_OK。 - callback:该方法执行时将调用该函数。
- err:如果方法失败,将抛出此错误。
 
 
以下示例说明了Node.js中的fs.access()方法:
范例1:本示例显示了文件读写权限的测试。
// Node.js program to demonstrate the 
// fs.access() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
// Allowing only read permission 
console.log("Giving only read permission to the user"); 
fs.chmodSync("example_file.txt", fs.constants.S_IRUSR); 
  
// Test the read permission 
fs.access('example_file.txt', fs.constants.R_OK, (err) => { 
  console.log('\n> Checking Permission for reading the file'); 
  if (err) 
    console.error('No Read access'); 
  else
    console.log('File can be read'); 
}); 
  
// Test both the read and write permissions 
fs.access('example_file.txt', fs.constants.R_OK  
                  | fs.constants.W_OK, (err) => { 
  console.log('\n> Checking Permission for reading" 
                          + " and writing to file'); 
  if (err) 
    console.error('No Read and Write access'); 
  else
    console.log('File can be read and written'); 
});输出:
Giving only read permission to the user > Checking Permission for reading the file File can be read > Checking Permission for reading and writing to file No Read and Write access
范例2:此示例显示文件的测试(如果存在)。
// Node.js program to demonstrate the 
// fs.access() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
// Test the if the file exists 
fs.access('example_file.txt', fs.constants.F_OK, (err) => { 
  console.log('\n> Checking if the file exists'); 
  
  if (err) { 
    console.error('File does not exist'); 
  
    // Create the file 
    console.log('\nCreating the file'); 
    fs.writeFileSync("example_file2.txt", "Test File"); 
  
    // Test the if the file exists again 
    fs.access('example_file2.txt', fs.constants.F_OK, (err) => { 
      console.log('\n> Checking if the file exists'); 
      if (err) 
        console.error('File does not exist'); 
      else { 
        console.log('File does exist'); 
      } 
    }); 
  } 
  else { 
    console.log('File does exist');     
  } 
});输出:
> Checking if the file exists File does not exist Creating the file > Checking if the file exists File does exist
参考: https://nodejs.org/api/fs.html#fs_fs_access_path_mode_callback
相关用法
- Node.js GM flop()用法及代码示例
 - Node.js GM spread()用法及代码示例
 - Node.js GM gamma()用法及代码示例
 - Node.js GM minify()用法及代码示例
 - Node.js GM magnify()用法及代码示例
 - Node.js GM modulate()用法及代码示例
 - Node.js GM gaussian()用法及代码示例
 - Node.js GM shave()用法及代码示例
 - Node.js GM emboss()用法及代码示例
 
注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 Node.js | fs.access() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
