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


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


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

注意:Windows平台仅支持更改写权限。它还不支持区分用户权限,组权限或其他权限。

用法:



fs.chmod( path, mode, callback )

参数:此方法接受上述和以下所述的三个参数:

  • path:它是一个字符串,Buffer或URL,表示必须更改其权限的文​​件的路径。
  • mode:它是字符串或八进制整数常量,表示要授予的权限。逻辑OR运算符可用于分隔多个权限。
  • callback:该方法执行时将调用该函数。
    • err:如果方法失败,将抛出此错误。

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

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

// Node.js program to demonstrate the 
// fs.chmod() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
// Grant only read permission to user 
console.log("Granting only read access to user"); 
  
fs.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); 
  } 
  
  // Grant both read and write permission to user 
  console.log("\nGranting read and write access to user"); 
  fs.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 is an example text file.

Trying to write to file
Error Code:EACCES

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

Reading the file contents
This file has now been edited.

范例2:此示例显示了使用字符串常量和OR运算符来授予文件权限的用法。

// Node.js program to demonstrate the 
// fs.chmod() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
// Grant only read permission to user 
console.log("Granting only read access to user"); 
fs.chmod("example.txt", fs.constants.S_IRUSR, () => { 
  
  // 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"); 
  fs.chmod("example.txt", fs.constants.S_IRUSR | fs.constants.S_IWUSR, () => { 
  
    // Check the file mode 
    console.log("Current File Mode:", fs.statSync("example.txt").mode); 
  
    console.log("Trying to write to file"); 
    fs.writeFileSync('example.txt', "This file now has been edited."); 
  
    console.log("File Contents:", fs.readFileSync("example.txt", 'utf8')); 
  }); 
});

输出:

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

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

Granting both read and write permission to user
Trying to write to file
File Contents:This file now has been edited.

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




相关用法


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