Previous words
The path module contains a set of tools to process and convert file paths, which can be accessed by using the require(‘path‘)
module. This article describes the path module in detail
Path composition
"Path.dirname (P)"
Returns the directory where the path p is located
var path = require (' path '); Console.log (Path.dirname ('/foo/bar/baz/asdf/a.txt ')); /foo/bar/baz/asdfconsole.log (Path.dirname ('/foo/bar/baz/asdf/')); /foo/bar/bazconsole.log (Path.dirname (' c:/test/aaa ')); C:/test
"Path.basename (p[, ext])"
Returns the last part of the path, which is the file name. The parameter ext is the suffix content that needs to be cut off
var path = require (' path '); Console.log (Path.basename ('/foo/bar/baz/asdf/a.txt ')); A.txtconsole.log (Path.basename ('/foo/bar/baz/asdf/a.txt ', '. txt ')); Aconsole.log (Path.basename ('/foo/bar/baz/asdf/')); Asdfconsole.log (Path.basename (' c:/test/aaa ')); Aaa
"Path.extname (P)"
Returns the extension of the path p, from the last '. ' To the end of the string. If the last section does not have '. ', or the path is '. ' Begins, an empty string is returned
var path = require (' path '); Console.log (Path.extname ('/foo/bar/baz/asdf/a.txt ')); . Txtconsole.log (Path.extname ('/foo/bar/baz/asdf/a.txt.b ')); . Bconsole.log (Path.extname ('/foo/bar/baz/asdf/a. ')); . Console.log (Path.extname (' c:/test/aaa/. ')); ' Console.log (path.extname (' c:/test/aaa ')); ‘‘
Separator
"Path.sep"
Returns the file delimiter under the corresponding platform, win under ' \ ', *nix '/'
var path = require (' path '); Console.log (PATH.SEP); Win under \,*nix for/console.log (' Foo\\bar\\baz '. Split (PATH.SEP)); [' foo ', ' Bar ', ' Baz ']console.log (' Foo/bar/baz '. Split (PATH.SEP)); Win next returns [' Foo/bar/baz '], but will return [' foo ', ' Bar ', ' Baz '] under the *nix system
"Path.delimiter"
Returns the path delimiter under the corresponding platform, win under '; ', *nix ': '
var path = require (' path '); Console.log (Path.delimiter); Win under ";", *nix under ":" Console.log (PATH.SEP); Win under the \,*nix for the/
Standardization
"Path.normalize (P)"
Normalize the path to handle the redundant "..", ".", "/" characters. When multiple slashes are found, they are replaced by a slash. Reserved when a slash is included at the end of the path. Windows system uses backslashes
var path = require (' path '); Console.log (Path.normalize (' a/b/c/. /user/bin ')//a\b\user\binconsole.log (Path.normalize (' a/b/c///. /user/bin/')//a\b\user\binconsole.log (Path.normalize (' a/b/c/. /.. /user/bin ')//a\user\binconsole.log (Path.normalize (' a/b/c/. /.////.. /user/bin/. ')); /a\userconsole.log (Path.normalize (' a/b/c/. /.. /user/bin/. /.. /'));//aconsole.log (Path.normalize (' A. /.. /user/bin/. /.. /‘));//.. Console.log (Path.normalize (' A. /.. /user/bin/. /.. /.. /.. /‘));//.. \.. \.. Console.log (Path.normalize ('./a/. /./user/bin/./'));//user\bin\
"Path.join ([path1], [path2], [...]) 】
Combine multiple paths and convert to normalized paths
var path = require (' path '), Console.log (Path.join ('////./a ', ' b////c ', ' user/')),//\a\b\c\userconsole.log (Path.join ( ' A ', '. /.. /', ' user/'); \user\
Absolute and Relative
"Path.resolve ([from ...], to)"
The absolute path from the source address to the destination to, similar to executing a series of CD commands in the shell
Path.resolve (' Foo/bar ', '/tmp/file/', ' ... ', '/'. /subfile ')
Similar to:
CD FOO/BARCD/TMP/FILE/CD. CD A/.. /subfilepwd
[note] If a from or to parameter is an absolute path (such as ' e:/abc ', or a path that begins with "/"), the previous from parameter is ignored
var path = require (' path '); Console.log (Path.resolve ('. ', ' testfiles/. ', ' Trdlayer '));//d:\project\ TrdLayerconsole.log (Path.resolve ('.. ', ' testfiles ', ' a.txt '));//d:\testfiles\a.txtconsole.log (' D: ', ' abc ', ' d:/a ');//d:\aconsole.log (Path.resolve (' abc ', ' Ok.gif '));//d:\project\abc\ok.gifconsole.log (path.resolve (' abc ', ' ... ', '/'. /subfile ')); D:\project\subfile
"Path.isabsolute (Path)"
Path is an absolute path (such as ' e:/abc '), or a path that begins with "/", both return True
var path = require (' path '); Console.log (Path.isabsolute ('. /testfiles/seclayer ');//falseconsole.log (Path.isabsolute ('./join.js '));//falseconsole.log (' Path.isAbsolute (' Temp '));//falseconsole.log (Path.isabsolute ('/temp/. /..‘));/ /trueconsole.log (Path.isabsolute (' E:/GITHUB/NODEAPI/ABC/EFG '));//trueconsole.log (Path.isabsolute ('///temp123 ') );//true
"Path.relative (from, to)"
Gets the relative path from the to to, which can be seen as the opposite implementation of the Path.resolve
Path.resolve (from, path.relative (from)) = = Path.resolve (To)
var path = require (' path '), Console.log (path.relative (' c:\\\test ', ' c:\\\impl\\bbb ')),//. \impl\bbbconsole.log (path.relative (' c:/test/aaa ', ' c:/bbb ')); \.. \bbbconsole.log (path.relative (' c:/test/aaa ', ' d:/bbb '));//d:\bbb
Path module of Nodejs