fs.symlinkSync()方法用於同步創建到指定路徑的符號鏈接。此方法創建一個鏈接,使path指向target。相對目標相對於鏈接的父目錄。
用法:
fs.symlinkSync( target, path, type )
參數:此方法接受上述和以下所述的三個參數:
- target:它是一個字符串,緩衝區或URL,代表必須創建符號鏈接的路徑。
 - path:它是一個字符串,緩衝區或URL,代表將在其中創建符號鏈接的路徑。
 - type:它是一個字符串,代表要創建的符號鏈接的類型。可以用“文件”,“目錄”或“連接”指定。如果目標不存在,將使用“文件”。
 
以下示例說明了Node.js中的fs.symlinkSync()方法:
範例1:本示例創建到文件的符號鏈接。
// Node.js program to demonstrate the 
// fs.symlinkSync() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
console.log("Contents of the text file:"); 
console.log( 
  fs.readFileSync('example_file.txt', 'utf8') 
); 
  
fs.symlinkSync(__dirname + "\\example_file.txt", 
               "symlinkToFile", 'file'); 
  
console.log("\nSymlink created\n"); 
console.log("Contents of the symlink created:"); 
console.log( 
  fs.readFileSync('symlinkToFile', 'utf8') 
);輸出:
Contents of the text file: Hello Geeks Symlink created Contents of the symlink created: Hello Geeks
範例2:本示例創建到目錄的符號鏈接。
// Node.js program to demonstrate the 
// fs.symlinkSync() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
fs.symlinkSync(__dirname + "\\example_directory", 
               "symlinkToDir", 'dir'); 
  
console.log("Symlink created"); 
console.log("Symlink is a directory:",  
  fs.statSync("symlinkToDir").isDirectory() 
);輸出:
Symlink created Symlink is a directory:true
參考: https://nodejs.org/api/fs.html#fs_fs_symlinksync_target_path_type
相關用法
- Node.js GM drawCircle()用法及代碼示例
 - Node.js GM drawBezier()用法及代碼示例
 - Node.js GM drawPolyline()用法及代碼示例
 - Node.js GM drawArc()用法及代碼示例
 - Node.js GM drawEllipse()用法及代碼示例
 - Node.js GM monochrome()用法及代碼示例
 - Node.js GM equalize()用法及代碼示例
 - Node.js GM enhance()用法及代碼示例
 - Node.js GM drawPolygon()用法及代碼示例
 - Node.js GM whitePoint()用法及代碼示例
 
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 Node.js | fs.symlinkSync() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
