node.js中的fs.realpath方法使用說明
方法說明:
獲取真實(shí)路徑。
可以使用process.cwd解決相對路徑。
語法:
fs.realpath(path, [cache], [callback(err , resolvedPath)])
由于該方法屬于fs模塊,使用前需要引入fs模塊(var fs= require(“fs”) )
接收參數(shù):
path 路徑
cache 可選,一個文字的映射路徑可用于強(qiáng)制一個特定的路徑解決或避免額外的fs.stat需要知道真正的路徑對象。
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();
}
};
相關(guān)文章
Node.JS發(fā)送http請求批量檢查文件中的網(wǎng)頁地址、服務(wù)是否有效可用
這篇文章主要介紹了Node.JS發(fā)送http請求批量檢查文件中的網(wǎng)頁地址、服務(wù)是否有效可用,本文通過實(shí)例代碼文字說明給大家講解的非常詳細(xì),需要的朋友參考下2019-11-11
Node.JS段點(diǎn)續(xù)傳:Nginx配置文件分段下載功能的實(shí)現(xiàn)方法
在Node.JS中可以配置這個標(biāo)簽來實(shí)現(xiàn)文件的分段下載。這篇文章給大家介紹了Node.JS段點(diǎn)續(xù)傳:Nginx配置文件分段下載功能的實(shí)現(xiàn)方法,需要的朋友參考下吧2018-03-03
詳解基于node的前端項(xiàng)目編譯時內(nèi)存溢出問題
本篇文章主要介紹了基于node的前端項(xiàng)目編譯時內(nèi)存溢出問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
Nodejs實(shí)現(xiàn)內(nèi)網(wǎng)穿透服務(wù)
很多人都不知道什么是內(nèi)網(wǎng)穿透,就是公網(wǎng)客戶端,可以訪問局域網(wǎng)內(nèi)的服務(wù),本文詳細(xì)的介紹了原理以及實(shí)現(xiàn),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
node.js使用zlib模塊進(jìn)行數(shù)據(jù)壓縮和解壓操作示例
這篇文章主要介紹了node.js使用zlib模塊進(jìn)行數(shù)據(jù)壓縮和解壓操作,結(jié)合實(shí)例形式詳細(xì)分析了node.js基于zlib模塊創(chuàng)建數(shù)據(jù)流以及壓縮和解壓縮等相關(guān)操作技巧,需要的朋友可以參考下2020-02-02
Nodejs使用exceljs實(shí)現(xiàn)excel導(dǎo)入導(dǎo)出
在日常開發(fā)中,我們常需在后臺管理系統(tǒng)中實(shí)現(xiàn)數(shù)據(jù)的導(dǎo)入與導(dǎo)出功能,以便與?Excel?文件進(jìn)行交互,本文將使用使用exceljs實(shí)現(xiàn)excel導(dǎo)入導(dǎo)出功能,需要的可以參考下2024-03-03

