阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js fsPromises.access

      Node.js fsPromises.access(path[, mode])

      新增于: v10.0.0

      • path < string> | < Buffer> | < URL>
      • mode < integer> Default: fs.constants.F_OK
      • Returns:

      Tests a user’s permissions for the file or directory specified by path. The mode argument is an optional integer that specifies the accessibility checks to be performed. Check File Access Constants for possible values of mode. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. fs.constants.W_OK | fs.constants.R_OK).

      If the accessibility check is successful, the Promise is resolved with no value. If any of the accessibility checks fail, the Promise is rejected with an Error object. The following example checks if the file /etc/passwd can be read and written by the current process.

      const fs = require('fs');
      const fsPromises = fs.promises;
      
      fsPromises.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK)
        .then(() => console.log('can access'))
        .catch(() => console.error('cannot access'));
      

      Using fsPromises.access() to check for the accessibility of a file before calling fsPromises.open() is not recommended. Doing so introduces a race condition, since other processes may change the file’s state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file is not accessible.


      更多内容请参考:Node.js fs 文件系统,或者通过 点击对应菜单 进行查看;


      目录
      本文目录
      目录