node. JS (quad) path optimization (path optimization)

Source: Internet
Author: User

basic usage of 1.normalize functions

The normalize function converts a path that does not conform to a specification to a standard path, which is resolved in a path. , you can also remove the extra slash.

The following example:

    1. var path = require(' path ');
    2. var data = path. Normalize('/path///normalize/hi/.. ') );
    3. Console. Log(data);
Operation Result:
    1. '/path/normalize/'

basic usage of 2.join functions

The Join function joins the multiple paths that are passed into a standard path and formats them, returning the canonical path, avoiding the tedious use of manually stitching the path strings. The following example:

    1. var path = require(' path ');
    2. var data = path. Join('///you ', '/are ', '//beautiful ');
    3. Console. Log(data);
Operation Result:
    1. '/you/are/beautiful '

basic usage of 3.dirname functions

The DirName function is used to return the directory name in the path. The following example:

    1. var path = require(' path ');
    2. var data = path. DirName('/foo/strong/cool/nice ');
    3. Console. Log(data);
Operation Result:
    1. '/foo/strong/cool '
basic usage of 4.basename functions

The basename function returns the last part of the path and can be conditionally excluded. The following example:

Example 1:path.basename (' path string ');

Example 2:path.basename (' Path string ', ' [ext] ') < exclude [ext] suffix string >;

  1. var path = require(' path ');
  2. var data1 = path. basename('/foo/strong/basename/index.html ');
  3. var data2 = path. basename('/foo/strong/basename/index.html ','. html ');
  4. Console. Log(data1 + ' "and" ' + data2);
Operation Result:
    1. ' index.html ' and ' index '
basic usage of 5.### extname function

The Extname function returns the extension of the file in the path (in the last '. ') Start, return '. ' and '. ' All subsequent strings, such as no '. ', return an empty string). The following example:

    1. var path = require(' path ');
    2. var data = path. Extname(' index.html ');
    3. Console. Log(data);
Operation Result:
    1. '. html '

Node. JS (quad) path optimization (path optimization)

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.