node.js中的fs.realpathSync方法使用說明
方法說明:
同步版的 fs.realpath() 。
語法:
fs.realpathSync(path, [cache])
由于該方法屬于fs模塊,使用前需要引入fs模塊(var fs= require(“fs”) )
接收參數(shù):
path 路徑
cache 可選,一個文字的映射路徑可用于強制一個特定的路徑解決或避免額外的fs.stat需要知道真正的路徑對象。
例子:
var fs = require('fs');
// 點號表示當前文件所在路徑
var str = fs.realpathSync('.');
console.log(str);
源碼:
fs.realpathSync = function realpathSync(p, cache) {
// make p is absolute
p = pathModule.resolve(p);
if (cache && Object.prototype.hasOwnProperty.call(cache, p)) {
return cache[p];
}
var original = p,
seenLinks = {},
knownHard = {};
// current character position in p
var pos;
// the partial path so far, including a trailing slash if any
var current;
// the partial path without a trailing slash (except when pointing at a root)
var base;
// the partial path scanned in the previous round, with slash
var previous;
start();
function start() {
// Skip over roots
var m = splitRootRe.exec(p);
pos = m[0].length;
current = m[0];
base = m[0];
previous = '';
// On windows, check that the root exists. On unix there is no need.
if (isWindows && !knownHard[base]) {
fs.lstatSync(base);
knownHard[base] = true;
}
}
// walk down the path, swapping out linked pathparts for their real
// values
// NB: p.length changes.
while (pos < p.length) {
// find the next part
nextPartRe.lastIndex = pos;
var result = nextPartRe.exec(p);
previous = current;
current += result[0];
base = previous + result[1];
pos = nextPartRe.lastIndex;
// continue if not a symlink
if (knownHard[base] || (cache && cache[base] === base)) {
continue;
}
var resolvedLink;
if (cache && Object.prototype.hasOwnProperty.call(cache, base)) {
// some known symbolic link. no need to stat again.
resolvedLink = cache[base];
} else {
var stat = fs.lstatSync(base);
if (!stat.isSymbolicLink()) {
knownHard[base] = true;
if (cache) cache[base] = base;
continue;
}
// read the link if it wasn't read before
// dev/ino always return 0 on windows, so skip the check.
var linkTarget = null;
if (!isWindows) {
var id = stat.dev.toString(32) + ':' + stat.ino.toString(32);
if (seenLinks.hasOwnProperty(id)) {
linkTarget = seenLinks[id];
}
}
if (util.isNull(linkTarget)) {
fs.statSync(base);
linkTarget = fs.readlinkSync(base);
}
resolvedLink = pathModule.resolve(previous, linkTarget);
// track this, if given a cache.
if (cache) cache[base] = resolvedLink;
if (!isWindows) seenLinks[id] = linkTarget;
}
// resolve the link, then start over
p = pathModule.resolve(resolvedLink, p.slice(pos));
start();
}
if (cache) cache[original] = p;
return p;
};
相關文章
前端+nodejs+mysql實現(xiàn)前后端聯(lián)通的完整代碼
Node.js主要屬于后端技術,但也可以用于前端開發(fā)的某些場景,下面這篇文章主要介紹了前端+nodejs+mysql實現(xiàn)前后端聯(lián)通的完整代碼,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-04-04
NodeJS如何優(yōu)雅的實現(xiàn)Sleep休眠
這篇文章主要介紹了NodeJS如何優(yōu)雅的實現(xiàn)Sleep休眠問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-09-09
Nodejs中Express 常用中間件 body-parser 實現(xiàn)解析
這篇文章主要介紹了Nodejs中Express 常用中間件 body-parser 實現(xiàn)解析,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
NodeJs環(huán)境安裝與配置的實現(xiàn)步驟
本文主要介紹了NodeJs環(huán)境安裝與配置,包括配置環(huán)境和配置國內(nèi)鏡像,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-01-01
nodejs中內(nèi)置模塊fs,path常見的用法說明
這篇文章主要介紹了nodejs中內(nèi)置模塊fs,path常見的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11

