最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Symbolic

运维笔记admin11浏览0评论

Symbolic

Symbolic

放置当前目录以外的符号链接无效,因为它们始终指向其当前目录

我在Windows 10上,我缺少什么?

fs.symlink('./testFile', './testDir/testSymLink', function(err){       // creates a symbolic- link in the 'testDir' subfolder relative to the current directory 
    if(err) console.log(err);
});

fs.readlink('./testDir/testSymlink',function(err, links){              // reads the created symbolic link 
    if(err) console.log(err);
    console.log(links);                                                // -> '.\testFile'   (points to the current directory not to the parent directory)
});

fs.readFile('./testDir/testSymlink.txt', function(err, data){          // file doesn't exist
    if(err) console.log(err);                                          // -> ENOENT no such file or directory 
    console.log(data);                                                 // -> undefined 
});

创建了符号链接(我们可以读取它),但是指向其当前目录.\testFile,它应该指向其父目录,其中参考文件为..\testFile

回答如下:

symlink()方法的第一个参数是所创建的符号链接使用的目标位置。

重要的是,符号链接在传递时使用第一个参数! (不会将其解析为绝对路径!)

这里是棘手的部分,因为上面的第一个参数是./testFile,符号链接将使用此路径在其自己的目录(而不是父目录)中引用'testFile']

解决方案:

1:上面的代码可以像这样固定(testSymLink目录中的testDir引用父目录中的testFile

fs.symlink('../testFile', './testDir/testSymLink', function(err){       
    if(err) console.log(err);
});

2:在symlink()方法中将绝对路径作为第一个参数传递(这将为您节省很多麻烦!)

fs.symlink(/* absolute path */, './testDir/testSymLink', function(err){       
    if(err) console.log(err);
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论