📜  Node.js fs.access() 方法

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

Node.js fs.access() 方法

fs.access() 方法用于测试给定文件或目录的权限。可以使用文件访问常量将要检查的权限指定为参数。也可以通过使用按位 OR运算符创建具有多个文件常量的掩码来检查多个文件权限。
注意:不建议在调用 fs.open()、fs.readFile() 或 fs.writeFile() 之前使用 fs.access() 方法来检查文件的可访问性,因为它引入了竞争条件,因为测试后其他进程可能会更改文件状态。

句法:

fs.access( path, mode, callback )

参数:此方法接受三个参数,如上所述,如下所述:

  • path:它是一个String,Buffer或URL,表示必须对其进行权限测试的文件或目录的路径。
  • mode:它是一个整数值,表示要测试的权限。逻辑 OR运算符可用于分隔多个权限。它可以具有值 fs.constants.F_OK、fs.constants.R_OK、fs.constants.W_OK 和 fs.constants.X_OK。它是一个可选参数。默认值为 fs.constants.F_OK。
  • 回调:它是一个在方法执行时将被调用的函数。
    • err:如果方法失败会抛出一个错误。

下面的示例说明了 Node.js 中的fs.access() 方法
例1:本例展示了一个文件读写权限的测试。

javascript
// 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');
});


javascript
// 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');   
  }
});


输出:

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:此示例显示了对文件是否存在的测试。

javascript

// 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