Path模塊部分常用函數解析——NodeJS


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

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

  • 參數[...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])#

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字符串,將會返回'.' , 表示當前工作目錄

例子:

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

如果參數中有任何路徑片段不是字符串,將會拋出TypeError錯誤;

 

path.normalize(path)#

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: 

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

 

On Windows:

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

 

path.format(pathObject)#

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:

 1 // If `dir`, `root` and `base` are provided,
 2 // `${dir}${path.sep}${base}`
 3 // will be returned. `root` is ignored.
 4 path.format({
 5   root: '/ignored',
 6   dir: '/home/user/dir',
 7   base: 'file.txt'
 8 });
 9 // Returns: '/home/user/dir/file.txt'
10 
11 // `root` will be used if `dir` is not specified.
12 // If only `root` is provided or `dir` is equal to `root` then the
13 // platform separator will not be included. `ext` will be ignored.
14 path.format({
15   root: '/',
16   base: 'file.txt',
17   ext: 'ignored'
18 });
19 // Returns: '/file.txt'
20 
21 // `name` + `ext` will be used if `base` is not specified.
22 path.format({
23   root: '/',
24   name: 'file',
25   ext: '.txt'
26 });
27 // Returns: '/file.txt'

On Windows:

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM