1 path.normalize(path)
规范化路径。
// Import the path module
const path = require('path');
path1 = path.normalize("/users/admin/.");
// returns : \users\admin
path2 = path.normalize("/users/admin/..");
// returns : \users
path3 = path.normalize("/users/admin/../comments")
// returns : \users\comments
path4 = path.normalize("/users///admin///comments")
// returns : \users\admin\comments
2 path.join([path1][, path2][, …])
用于连接路径。该方法的主要用途在于,会正确使用当前系统的路径分隔符,Unix系统是"/“,Windows系统是”"。
path1 = path.join("/users/admin/","/hello");
// returns : \users\admin\hello
path2 = path.join("/users/admin/","../hello");
// returns : \users\hello
path3 = path.join("/users/admin/","..")
// returns ult: \users
3 path.isAbsolute(path)
判断参数 path 是否是绝对路径。
path1 = path.isAbsolute("/user/bash/"); //true
path2 = path.isAbsolute("user/bash/readme.md"); //false
path3 = path.isAbsolute("/user/bash/readme.md"); //true
path4 = path.isAbsolute(".."); //false
path5 = path.isAbsolute("\\user\\bash\\"); //true
4 path.dirname (path)
返回路径中代表文件夹的部分。
path1 = path.dirname ("/user/bash/"); // /user
5 path.basename(p[, ext])
返回路径中的最后一部分。
- ext :string 可选的文件扩展名。
尽管 Windows 通常以不区分大小写的方式处理文件名(包括文件扩展名),但是此函数不会这样。 例如, C:\文件.html 和 C:\文件.HTML 指向相同的文件,但是 basename 会将扩展名视为区分大小写的字符串:
path1 = path.basename("/user/bash/hello.js"); // hello.js
path1 = path.basename("/user/bash/hello.js",'.js'); // hello
path1 = path.basename("//user/bash/hello.js"); // hello.js
path1 = path.basename("/user/bash/hello.JS",'.js'); // hello.JS
6 path.extname(path)
返回路径中文件的后缀名。
path1 = path.extname('index.html'); // returns : '.html'
path2 = path.extname('index.'); // returns : '.'
path3 = path.extname('index'); // returns : ''
7 path.parse(path)
返回路径字符串的对象。
path1 = path.parse('C:/a/b/c/d.js');
/* returns : {
base: "c.js"
dir: "C:/a/b/x"
ext: ".js"
name: "c"
root: "C:/"
*/
}
8 path.format(pathObject)
从对象中返回路径字符串,和 path.parse 相反。该方法具有一些规则,其中一个路径属性比另一个路径属性具有更高的优先级:
- 如果提供了“dir”参数,则忽略路径对象的“root”参数。
- 如果提供了“base”参数,则忽略路径对象的“ext”和“name”参数。
参数:此函数接受包含路径详细信息的单个参数pathObject。它具有以下参数:
- dir:它指定路径对象的目录名称。
- root:它指定路径对象的根。
- base:它指定路径对象的基础。
- name:它指定路径对象的文件名。
- ext:它指定路径对象的文件扩展名。
// CASE 1
// If "dir", "root" and "base" are all given, "root" is ignored.
let path1 = path.format({
root:"C:\\ignored\\root",
dir:"website\\dist",
base:"index.html",
});
// returns : Path 1:website\dist\index.html
// CASE 2
// If "dir" is not specified then "root" will be used
// If only "root" is provided platform separator will not be included.
// "ext" will be ignored.
let path2 = path.format({
root:"C:\\",
base:"style.css",
ext:".ignored",
});
// returns : Path 2:C:\style.css
// CASE 3
// If "base" is not specified
// "name" and "ext" will be used
let path3 = path.format({
root:"website\\",
name:"main",
ext:".js",
});
// returns : Path 3:website\main.js
9 path.sep
平台的文件路径分隔符,‘\’ 或 ‘/’。
'foo\\bar\\baz'.split(path.sep)
// returns: ['foo', 'bar', 'baz']
10 path.resolve()
将一些的 路径/路径段 解析为绝对路径。
path.resolve('./geea') // returns : "C:\Users\df\Desktop\upy\geea"
path.resolve('./geea','..') // returns :"C:\Users\df\Desktop\upy"
参考: http://www.manongjc.com/detail/30-ebhxdstwwsioyef.html
https://vimsky.com/examples/usage/node-js-path-format-method.html