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
相關用法
- Node.js GM drawRectangle()用法及代碼示例
 - Node.js GM minify()用法及代碼示例
 - Node.js GM magnify()用法及代碼示例
 - Node.js GM whiteThreshold()用法及代碼示例
 - Node.js GM whitePoint()用法及代碼示例
 - Node.js GM write()用法及代碼示例
 - Node.js GM drawBezier()用法及代碼示例
 - Node.js GM drawPolyline()用法及代碼示例
 - Node.js GM drawArc()用法及代碼示例
 - Node.js GM drawLine()用法及代碼示例
 
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 Node.js | fs.chmod() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
