A deep understanding of the path module of node. js, node. jspath

Source: Internet
Author: User
Tags file separator

A deep understanding of the path module of node. js, node. jspath

Node path Module

// Reference this module var path = require ("path ");

1. parse the path to obtain the canonicalized path format.

// For the window system, the directories are separated into '\'. For UNIX systems, the separator is '/', '.. 'back to the upper level; // and \ are uniformly converted // path. normalize (p); var myPath = path. normalize (_ dirname + '/test/a/B //.. /c/utilyou.mp3 '); console. log (myPath); // windows: E: \ workspace \ NodeJS \ app \ fs \ test \ a \ c \ utilyou.pdf

2. Path combination and merge. The path does not contain directory separators at last.

// Path. join ([path1], [path2] .. [pathn]);/*** [path1] path or a directory character, */var path1 = 'path1', path2 = 'path2 // pp \\', path3 = '.. /path3 '; var myPath = path. join (path1, path2, path3); console. log (myPath); // path1 \ path2 \ path3

3. Obtain the absolute path

// Path. resolve (path1, [path2] .. [pathn]); // starts with an application, parse an absolute path/*** path according to the parameter string. At least one path string value * [pathn] Optional path string */var myPath = path. resolve ('path1', 'path2', 'A/B \ c/'); console. log (myPath); // E: \ workspace \ NodeJS \ path1 \ path2 \ a \ B \ c

4. Obtain the relative path

// Path. relative (from, to); // obtain the relative relationship between two paths/*** from current path, in addition, the return value of the method is based on the relative path * to specified by from, */var from = 'C: \ from \ \\', to = 'C:/test/B '; var _ path = path. relative (from, to); console. log (_ path );//.. \.. \ test \ B; indicates the relative path from

5. path. dirname (p)

// Obtain the directory name var myPath = path. dirname (_ dirname + '/test/util youning'); console. log (myPath );

6. path. basename (path, [ext])

// Obtain the file name in the path. The suffix is optional. Use '. ext, the suffix is not included in the returned value; var myPath = path. basename (_ dirname + '/test/util youxian', 'shanghai'); console. log (myPath );

7. path. extname (path) // obtain the extension in the path. If '.' is not found, null is returned.

8. path. sep properties // return the file Separator in the operating system; window is '\'; Unix is '/'

9. path. delimiter properties // return the directory Separator in the operating system, for example, window is ';', Unix is ':'

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.