fs.access(path[, mode], callback)
| 版本 | 变化 | 
|---|---|
| v18.0.0 | 将无效回调传递给   | 
| v7.6.0 | 
  | 
| v6.3.0 | 
  | 
| v0.11.15 | 添加于:v0.11.15  | 
参数
path<string> | <Buffer> | <URL>mode<integer> 默认:fs.constants.F_OKcallback<Function>err<Error>
测试用户对 path 指定的文件或目录的权限。 mode 参数是一个可选整数,用于指定要执行的可访问性检查。 mode 应该是值 fs.constants.F_OK 或由 fs.constants.R_OK 、 fs.constants.W_OK 和 fs.constants.X_OK 中的任何一个的按位或组成的掩码(例如 fs.constants.W_OK | fs.constants.R_OK )。检查 File access constants 以获取 mode 的可能值。
最后一个参数 callback 是一个回调函数,使用可能的错误参数调用。如果任何可访问性检查失败,错误参数将是一个Error 对象。以下示例检查package.json 是否存在,以及它是否可读或可写。
import { access, constants } from 'node:fs';
const file = 'package.json';
// Check if the file exists in the current directory.
access(file, constants.F_OK, (err) => {
  console.log(`${file} ${err ? 'does not exist' : 'exists'}`);
});
// Check if the file is readable.
access(file, constants.R_OK, (err) => {
  console.log(`${file} ${err ? 'is not readable' : 'is readable'}`);
});
// Check if the file is writable.
access(file, constants.W_OK, (err) => {
  console.log(`${file} ${err ? 'is not writable' : 'is writable'}`);
});
// Check if the file is readable and writable.
access(file, constants.R_OK | constants.W_OK, (err) => {
  console.log(`${file} ${err ? 'is not' : 'is'} readable and writable`);
});
在调用 fs.open() 、 fs.readFile() 或 fs.writeFile() 之前,不要使用 fs.access() 检查文件的可访问性。这样做会引入竞争条件,因为其他进程可能会在两次调用之间更改文件的状态。相反,用户代码应该直接打开/读取/写入文件并处理文件不可访问时引发的错误。
写(不推荐)
import { access, open, close } from 'node:fs';
access('myfile', (err) => {
  if (!err) {
    console.error('myfile already exists');
    return;
  }
  open('myfile', 'wx', (err, fd) => {
    if (err) throw err;
    try {
      writeMyData(fd);
    } finally {
      close(fd, (err) => {
        if (err) throw err;
      });
    }
  });
});
写(推荐)
import { open, close } from 'node:fs';
open('myfile', 'wx', (err, fd) => {
  if (err) {
    if (err.code === 'EEXIST') {
      console.error('myfile already exists');
      return;
    }
    throw err;
  }
  try {
    writeMyData(fd);
  } finally {
    close(fd, (err) => {
      if (err) throw err;
    });
  }
});
阅读(不推荐)
import { access, open, close } from 'node:fs';
access('myfile', (err) => {
  if (err) {
    if (err.code === 'ENOENT') {
      console.error('myfile does not exist');
      return;
    }
    throw err;
  }
  open('myfile', 'r', (err, fd) => {
    if (err) throw err;
    try {
      readMyData(fd);
    } finally {
      close(fd, (err) => {
        if (err) throw err;
      });
    }
  });
});
阅读(推荐)
import { open, close } from 'node:fs';
open('myfile', 'r', (err, fd) => {
  if (err) {
    if (err.code === 'ENOENT') {
      console.error('myfile does not exist');
      return;
    }
    throw err;
  }
  try {
    readMyData(fd);
  } finally {
    close(fd, (err) => {
      if (err) throw err;
    });
  }
});
上面的"not recommended" 示例检查可访问性,然后使用该文件; "recommended" 示例更好,因为它们直接使用文件并处理错误(如果有)。
通常,仅当文件不会被直接使用时才检查文件的可访问性,例如当它的可访问性是来自另一个进程的信号时。
在 Windows 上,目录上的access-control 策略 (ACL) 可能会限制对文件或目录的访问。但是,fs.access() 函数不会检查 ACL,因此即使 ACL 限制用户读取或写入路径,它也可能报告路径是可访问的。
相关用法
- Node.js fs.access()用法及代码示例
 - Node.js fs.accessSync()用法及代码示例
 - Node.js fs.accessSync(path[, mode])用法及代码示例
 - Node.js fs.appendFileSync()用法及代码示例
 - Node.js fs.appendFile()用法及代码示例
 - Node.js fs.appendFileSync(path, data[, options])用法及代码示例
 - Node.js fs.appendFile(path, data[, options], callback)用法及代码示例
 - Node.js fs.filehandle.datasync()用法及代码示例
 - Node.js fs.chmod()用法及代码示例
 - Node.js fs.read()用法及代码示例
 - Node.js fs.Dirent.isFile()用法及代码示例
 - Node.js fs.Dir.closeSync()用法及代码示例
 - Node.js fs.fchmodSync()用法及代码示例
 - Node.js fs.symlink(target, path[, type], callback)用法及代码示例
 - Node.js fs.constants用法及代码示例
 - Node.js fs.mkdir()用法及代码示例
 - Node.js fs.mkdirSync()用法及代码示例
 - Node.js fs.fdatasync()用法及代码示例
 - Node.js fs.Dirent.isFIFO()用法及代码示例
 - Node.js fs.copyFile()用法及代码示例
 - Node.js fs.writeSync()用法及代码示例
 - Node.js fs.symlink()用法及代码示例
 - Node.js fs.truncate()用法及代码示例
 - Node.js fs.openSync()用法及代码示例
 - Node.js fs.filehandle.write()用法及代码示例
 
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 fs.access(path[, mode], callback)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
