node. JS Path Object

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.