fsPromises.symlink()方法用于创建到指定路径的符号链接,然后在成功时不带任何参数地解析Promise。这将创建一个链接,使路径指向目标。相对目标是相对于链接的父目录的。
用法:
fsPromises.symlink( target, path, type )
参数:此方法接受上述和以下所述的三个参数:
- target:它是一个字符串,缓冲区或URL,代表必须创建符号链接的路径。
 - path:它是一个字符串,缓冲区或URL,代表将在其中创建符号链接的路径。
 - type:它是一个字符串,代表要创建的符号链接的类型。可以用‘file’,‘dir’或‘junction’来指定。如果目标不存在,将使用‘file’。
 
type参数仅在Windows平台上使用,可以是‘dir’,‘file’或‘junction’之一。 Windows连接点要求目标路径是绝对路径。使用‘junction’时,目标参数将自动归一化为绝对路径。
例:此示例说明了Node.js中的fsPromises.symlink()方法:
文件名:index.js
// Node.js program to demonstrate the  
// fsPromises.symlink method  
    
// Import the filesystem module  
const fs = require('fs');  
const fsPromises = fs.promises; 
    
console.log("Contents of the text file:");  
console.log(fs.readFileSync( 
        'example_file.txt', 'utf8'));  
    
fsPromises.symlink(__dirname +  
    "\\example_file.txt", "symlinkToFile", 'file')  
.then(function() { 
  console.log("\nSymlink created\n");  
  console.log("Contents of the symlink created:");  
  console.log(fs.readFileSync('symlinkToFile', 'utf8'));  
}) 
.catch(function(error) { 
  console.log(error); 
});运行此程序的步骤:使用以下命令运行index.js文件:
node index.js
输出:
Contents of the text file: Hello Geeks Symlink created Contents of the symlink created: Hello Geeks
参考: https://nodejs.org/api/fs.html#fs_fspromises_symlink_target_path_type
相关用法
- Node.js console.timeLog()用法及代码示例
 - Node.js GM chop()用法及代码示例
 - Node.js GM drawArc()用法及代码示例
 - Node.js GM edge()用法及代码示例
 - Node.js GM drawLine()用法及代码示例
 - Node.js GM bordercolor()用法及代码示例
 - Node.js GM border()用法及代码示例
 - Node.js GM drawPolyline()用法及代码示例
 - Node.js GM channel()用法及代码示例
 - Node.js GM whitePoint()用法及代码示例
 - Node.js GM operator()用法及代码示例
 - Node.js GM transparent()用法及代码示例
 
注:本文由纯净天空筛选整理自nitin_sharma大神的英文原创作品 Node.js | fsPromises.symlink() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
