node.js中的fs.realpath方法使用說(shuō)明
方法說(shuō)明:
獲取真實(shí)路徑。
可以使用process.cwd解決相對(duì)路徑。
語(yǔ)法:
fs.realpath(path, [cache], [callback(err , resolvedPath)])
由于該方法屬于fs模塊,使用前需要引入fs模塊(var fs= require(“fs”) )
接收參數(shù):
path 路徑
cache 可選,一個(gè)文字的映射路徑可用于強(qiáng)制一個(gè)特定的路徑解決或避免額外的fs.stat需要知道真正的路徑對(duì)象。
callback 回調(diào)
err 異常
resolvedPath 真實(shí)地址
例子:
var cache = {'/etc':'/private/etc'};
fs.realpath('/etc/passwd', cache, function (err, resolvedPath) {
if (err) throw err;
console.log(resolvedPath);
});
源碼:
fs.realpath = function realpath(p, cache, cb) {
if (!util.isFunction(cb)) {
cb = maybeCallback(cache);
cache = null;
}
// make p is absolute
p = pathModule.resolve(p);
if (cache && Object.prototype.hasOwnProperty.call(cache, p)) {
return process.nextTick(cb.bind(null, null, 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.lstat(base, function(err) {
if (err) return cb(err);
knownHard[base] = true;
LOOP();
});
} else {
process.nextTick(LOOP);
}
}
// walk down the path, swapping out linked pathparts for their real
// values
function LOOP() {
// stop if scanned past end of path
if (pos >= p.length) {
if (cache) cache[original] = p;
return cb(null, p);
}
// 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)) {
return process.nextTick(LOOP);
}
if (cache && Object.prototype.hasOwnProperty.call(cache, base)) {
// known symbolic link. no need to stat again.
return gotResolvedLink(cache[base]);
}
return fs.lstat(base, gotStat);
}
function gotStat(err, stat) {
if (err) return cb(err);
// if not a symlink, skip to the next path part
if (!stat.isSymbolicLink()) {
knownHard[base] = true;
if (cache) cache[base] = base;
return process.nextTick(LOOP);
}
// stat & read the link if not read before
// call gotTarget as soon as the link target is known
// dev/ino always return 0 on windows, so skip the check.
if (!isWindows) {
var id = stat.dev.toString(32) + ':' + stat.ino.toString(32);
if (seenLinks.hasOwnProperty(id)) {
return gotTarget(null, seenLinks[id], base);
}
}
fs.stat(base, function(err) {
if (err) return cb(err);
fs.readlink(base, function(err, target) {
if (!isWindows) seenLinks[id] = target;
gotTarget(err, target);
});
});
}
function gotTarget(err, target, base) {
if (err) return cb(err);
var resolvedLink = pathModule.resolve(previous, target);
if (cache) cache[base] = resolvedLink;
gotResolvedLink(resolvedLink);
}
function gotResolvedLink(resolvedLink) {
// resolve the link, then start over
p = pathModule.resolve(resolvedLink, p.slice(pos));
start();
}
};
- Node.js path模塊,獲取文件后綴名操作
- node.JS路徑解析之PATH模塊使用方法詳解
- 詳解Node.js中path模塊的resolve()和join()方法的區(qū)別
- 使用JavaScript實(shí)現(xiàn)node.js中的path.join方法
- 深入理解node.js之path模塊
- Node.js中路徑處理模塊path詳解
- node.js中的fs.realpathSync方法使用說(shuō)明
- node.js中的path.basename方法使用說(shuō)明
- node.js中的path.dirname方法使用說(shuō)明
- Node.js開(kāi)發(fā) path路徑模塊詳解
相關(guān)文章
Node.js和Express中設(shè)置TypeScript的實(shí)現(xiàn)步驟
本文主要介紹了Node.js和Express中設(shè)置TypeScript的實(shí)現(xiàn)步驟文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11NodeJS連接MongoDB數(shù)據(jù)庫(kù)時(shí)報(bào)錯(cuò)的快速解決方法
下面小編就為大家?guī)?lái)一篇NodeJS連接MongoDB數(shù)據(jù)庫(kù)時(shí)報(bào)錯(cuò)的快速解決方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考2016-05-05詳解node服務(wù)器中打開(kāi)html文件的兩種方法
本篇文章主要介紹了詳解node服務(wù)器中打開(kāi)html文件的兩種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09NodeJs+MySQL實(shí)現(xiàn)注冊(cè)登錄功能
這篇文章主要為大家詳細(xì)介紹了NodeJs+MySQL實(shí)現(xiàn)注冊(cè)登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04nodejs+mongodb+vue前后臺(tái)配置ueditor的示例代碼
本篇文章主要介紹了nodejs+mongodb+vue前后臺(tái)配置ueditor的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01