node.js中的fs.realpathSync方法使用说明

前端之家收集整理的这篇文章主要介绍了node.js中的fs.realpathSync方法使用说明前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

方法说明:

同步版的

语法:

代码如下:

由于该方法属于fs模块,使用前需要引入fs模块(var fs= require(“fs”) )

接收参数:

path 路径

cache 可选,一个文字的映射路径可用于强制一个特定的路径解决或避免额外的fs.stat需要知道真正的路径对象。

例子:

代码如下:
// 点号表示当前文件所在路径
var str = fs.realpathSync('.');
console.log(str);

源码:

代码如下:
IoUs 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; };

原文链接:https://www.f2er.com/nodejs/56893.html

猜你在找的Node.js相关文章