Path模块部分常用函数解析——NodeJS

时间:2023-03-10 01:18:29
Path模块部分常用函数解析——NodeJS

官网地址:https://nodejs.org/api/path.html

path.resolve([...paths])#

Added in: v0.3.4
  • 参数[...paths]: <String> 参数是一个路径序列或路径片段
  • 返回: <String>

功能:该函数将一个路径序列或路径片段组合成一个绝对路径;

path.resolve([path1][, path2][, ...]) 从右向左依次拼接该路径序列,直到构成一个绝对路径。例如,输入参数:/foo, /bar, baz, 调用函数path.resolve('/foo', '/bar', 'baz')后返回结果是 /bar/baz;

如果处理完所有参数仍然没有构成一个绝对路径,就使用当前工作目录的绝对路径;结果返回的路径是经normalized后的,尾随斜线是没有的,除非是根路径;

Zero-length path segments are ignored.

如果路径序列中没有可用的路径片段,该函数将返回当前工作目录的绝对路径;

例子:

path.resolve('/foo/bar', './baz')
// Return: '/foo/bar/baz' path.resolve('/foo/bar', '/tmp/file/')
// Return: '/tmp/file' path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
//如果当前工作目录是/home/myself/node,
//返回结果是: '/home/myself/node/wwwroot/static_files/gif/image.gif'

  如果有任何参数不是字符串,将会抛出TypeError错误;

path.join([...paths])#

Added in: v0.1.16

The path.join() method joins all given path segments together using the platform specific separator as a delimiter, then normalizes the resulting path.

作用:该函数使用指定分隔符将参数中所有路径片段连接到一起,并返回normalize后的结果路径。

Zero-length path segments are ignored. If the joined path string is a zero-length string then '.'will be returned, representing the current working directory.

如果连接的参数长度为0字符串,将会返回'.' , 表示当前工作目录

例子:

 path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
//Return: '/foo/bar/baz/asdf' path.join('foo', {}, 'bar')
//throws TypeError:Arguments to path.join must be strings

如果参数中有任何路径片段不是字符串,将会抛出TypeError错误;

path.normalize(path)#

Added in: v0.1.23

The path.normalize() method normalizes the given path, resolving '..' and '.' segments.

作用:标准化路径,处理'..'和'.'

When multiple, sequential path segment separation characters are found (e.g. / on POSIX and \on Windows), they are replaced by a single instance of the platform specific path segment separator. 后缀分隔符将保留;

If the path is a zero-length string, '.' is returned, representing the current working directory.

如果path是长度为0的字符串,将会返回'.' , 表示当前工作目录

For example on POSIX:

 path.normalize('/foo/bar//baz/asdf/quux/..')
// Returns: '/foo/bar/baz/asdf'

On Windows:

 path.normalize('C:\\temp\\\\foo\\bar\\..\\')
//Returns: 'C:\\temp\\foo\\'

path.format(pathObject)#

Added in: v0.11.15

The path.format() method returns a path string from an object. This is the opposite ofpath.parse().

When providing properties to the pathObject remember that there are combinations where one property has priority over another:

  • pathObject.root is ignored if pathObject.dir is provided
  • pathObject.ext and pathObject.name are ignored if pathObject.base exists

For example, on POSIX:

 // If `dir`, `root` and `base` are provided,
// `${dir}${path.sep}${base}`
// will be returned. `root` is ignored.
path.format({
root: '/ignored',
dir: '/home/user/dir',
base: 'file.txt'
});
// Returns: '/home/user/dir/file.txt' // `root` will be used if `dir` is not specified.
// If only `root` is provided or `dir` is equal to `root` then the
// platform separator will not be included. `ext` will be ignored.
path.format({
root: '/',
base: 'file.txt',
ext: 'ignored'
});
// Returns: '/file.txt' // `name` + `ext` will be used if `base` is not specified.
path.format({
root: '/',
name: 'file',
ext: '.txt'
});
// Returns: '/file.txt'

On Windows:

 path.format({
dir : "C:\\path\\dir",
base : "file.txt"
});
// Returns: 'C:\\path\\dir\\file.txt'