Node.js path.join()用法及代码示例

您所在的位置:网站首页 join,是什么意思 Node.js path.join()用法及代码示例

Node.js path.join()用法及代码示例

2023-04-11 03:42| 来源: 网络整理| 查看: 265

path.join()方法用于使用platform-specific定界符连接多个path-segments,以形成单个路径。加入后,最终路径将被规范化。 path-segments使用逗号分隔的值指定。

用法:

path.join( [...paths] )

参数:该函数接受上面提到并在下面描述的一个参数:

paths:它是逗号分隔的一系列路径,这些路径将连接在一起以构成最终路径。

返回值:它返回一个带有完整标准化路径的字符串,其中包含所有段。

以下示例说明了Node.js中的path.join()方法:

范例1:

Node.js // Node.js program to demonstrate the    // path.join() Method      // Import the path module const path = require('path');     // Joining 2 path-segments path1 = path.join("users/admin/files", "index.html"); console.log(path1)     // Joining 3 path-segments path2 = path.join("users", "geeks/website", "index.html"); console.log(path2)     // Joining with zero-length paths path3 = path.join("users", "", "", "index.html"); console.log(path3)

输出:

users\admin\files\index.html users\geeks\website\index.html users\index.html

范例2:

Node.js // Node.js program to demonstrate the    // path.join() Method      // Import the path module const path = require('path');     // Normalizing of the final path path1 = path.join("users", "..", "files", "readme.md"); console.log(path1)     // Zero length final path // returns a period (.) path2 = path.join("users", ".."); console.log(path2)     // Getting the directory path one folder above console.log("Current Directory:", __dirname); path3 = path.join(__dirname, ".."); console.log("Directory above:", path3)

输出:

files\readme.md . Dirname: G:\tutorials\nodejs-path-join Directory above:G:\tutorials

参考: https://nodejs.org/api/path.html#path_path_join_paths



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3