The path object in Nodejs, which is used to manipulate the directory object, improves development efficiency.
Using the Nodejs Path command is similar to using a shell script command under Linux.
Introduce the Path object:
var path = require (' path ');
A more practical approach:
Format path Path.normalize (p)
Features: formats non-conforming paths, simplifies processing of complex path judgments among developers
Example:
Path.normalize ('/foo/bar//baz/asdf/quux/. '); = '/FOO/BAR/BAZ/ASDF '
Path merge Path.join ([path1], [path2], [...])
Feature: Concatenate all names in Path.seq, then format them with Normailze
Example:
Path.join ('///foo ', ' Bar ', '//baz/asdf ', ' quux ', ' ... '); = '/FOO/BAR/BAZ/ASDF '
Path Finder path.resolve ([from ...], to)
Features: equivalent to the constant call system of the CD command
Example:
Path.resolve (' Foo/bar ', '/tmp/file/', ' ... ', '/'. /subfile ')//equivalent: CD FOO/BARCD/TMP/FILE/CD. CD A/.. /subfilepwd
Relative path Path.relative (from, to)
Feature: Returns the relative position string of a path relative to another path, equivalent to: Path.resolve (from, path.relative (from)) = = Path.resolve (To)
Example:
Path.relative (' c:\\orandea\\test\\aaa ', ' c:\\orandea\\impl\\bbb ') = '. \\.. \\IMPL\\BBB ' path.relative ('/data/orandea/test/aaa ', '/data/orandea/impl/bbb ') = '. /.. /IMPL/BBB '
Folder name Path.dirname (p)
Feature: Return the folder name where the path is located
Example:
Path.dirname ('/foo/bar/baz/asdf/quux ') = '/foo/bar/baz/asdf '
File name Path.basename (p, [ext])
Features: Returns the specified file name and returns the result to exclude the [ext] suffix string
Example:
Path.basename ('/foo/bar/baz/asdf/quux.html ') = ' quux.html ' path.basename ('/foo/bar/baz/asdf/quux.html ', '. html ') = ' Quux '
Extension name Path.extname (p)
Feature: Returns the extension name of the specified file name
Example:
Path.extname (' index.html ') = '. html ' path.extname (' Index. ') = '. ' Path.extname (' index ') = '
File separators for specific platforms PATH.SEP
Features: Gets the delimiter of the file path, mainly related to the operating system
Example:
Linux:
' Foo/bar/baz '. Split (path.sep) =>[' foo ', ' Bar ', ' Baz ']
window:
' Foo\\bar\\baz '. Split (path.sep) =>[' foo ', ' Bar ', ' Baz ']
Path delimiter for a specific platform Path.delimiter
The path delimiter for a particular platform, ' ;
or ' :
.
Examples on *nix:
Process.env.PATH.split (Path.delimiter)//returns['/usr/bin ', '/bin ', '/usr/sbin ', '/sbin ', '/usr/local/bin ']
Examples on Windows:
Console.log (Process.env.PATH)//' C:\Windows\system32; C:\Windows; C:\Program files\nodejs\ ' Process.env.PATH.split (path.delimiter)//returns[' C:\Windows\system32 ', ' C:\Windows ', ' C : \program Files\nodejs\ '] Process.env.PATH.split (path.delimiter)//returns[' C:\Windows\system32 ', ' C:\Windows ', ' C : \program Files\nodejs\ ']
node. JS Path Object