fs.realpathSync()方法用於同步計算給定路徑的規範路徑名。它通過解決.
,..
以及路徑中的符號鏈接並返回已解析的路徑。
用法:
fs.realpathSync( path, options )
參數:此方法接受上述和以下所述的兩個參數:
- path:它包含必須解析的目錄的路徑。它可以是字符串,緩衝區或URL。
- options:它是一個字符串或對象,可用於指定將影響操作的可選參數。它具有一個可選參數:
- encoding:它是一個字符串,用於定義解析路徑的編碼。
返回值:它返回一個代表解析路徑的字符串或緩衝區。
以下示例說明了Node.js中的fs.realpathSync()方法:
範例1:
// Node.js program to demonstrate the
// fs.realpathSync() method
// Import the filesystem module
const fs = require('fs');
console.log("Current Directory Path:", __dirname);
// Finding the canonical path
// one directory up
path1 = __dirname + "\\..";
resolvedPath = fs.realpathSync(path1);
console.log("One directory up resolved path is:",
resolvedPath);
// Finding the canonical path
// two directories up
path2 = __dirname + "\\..\\..";
resolvedPath = fs.realpathSync(path2);
console.log("Two directories up resolved path is:",
resolvedPath);
輸出:
Current Directory Path:G:\tutorials\nodejs-fs-realPathSync One directory up resolved path is: G:\tutorials Two directories up resolved path is: G:\
範例2:
// Node.js program to demonstrate the
// fs.realpathSync() method
// Import the filesystem module
const fs = require('fs');
path = __dirname + "\\..";
// Getting the canonical path is utf8 encoding
resolvedPath = fs.realpathSync(path, { encoding:"utf8" });
console.log("The resolved path is:", resolvedPath);
// Getting the canonical path is hex encoding
resolvedPath = fs.realpathSync(path, { encoding:"hex" });
console.log("The resolved path is:", resolvedPath);
// Getting the canonical path is base64 encoding
resolvedPath = fs.realpathSync(path, { encoding:"base64" });
console.log("The resolved path is:", resolvedPath);
輸出:
The resolved path is: G:\tutorials The resolved path is: 473a5c7475746f7269616c73 The resolved path is: RzpcdHV0b3JpYWxz
參考: https://nodejs.org/api/fs.html#fs_fs_realpathsync_path_options
相關用法
- Node.js GM equalize()用法及代碼示例
- Node.js GM paint()用法及代碼示例
- Node.js GM edge()用法及代碼示例
- Node.js GM implode()用法及代碼示例
- Node.js GM recolor()用法及代碼示例
- Node.js GM randomThreshold()用法及代碼示例
- Node.js GM orderedDither()用法及代碼示例
- Node.js GM drawLine()用法及代碼示例
- Node.js GM drawCircle()用法及代碼示例
- Node.js GM drawEllipse()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 Node.js | fs.realpathSync() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。