node. JS Learning Day fifth-Path optimization

Source: Internet
Author: User

1. Path optimization basic usage of the path module

This module contains a set of tools for processing and converting file paths, for processing directory objects, to improve user development efficiency, let us quickly understand the path object of some common methods!

2. Basic usage of normalize function

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:

var path = require (' path ');    var data = path.normalize ('/path///normalize/hi/. '); Console.log (data);
Operation Result:
        '/path/normalize/'

See the above example, is not feeling very easy, quickly find a path to try the effect of it (step-by-walk yo)!

The slash in the path after normalization is under Windows System, and under the Linux system is/. If you want to make sure that you use/as a path delimiter under any system, you need to replace it with replace.

3. Basic usage of Join function

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:

var path = require (' path ');    var data = Path.join ('///you ', '/are ', '//beautiful '); console.log (data);
Operation Result:
        '/you/are/beautiful '

Look at the above example, is not the feeling and the last class is very similar to, quickly try it!

The join function can be used for stitching and formatting paths.

4. Basic usage of dirname function

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

var path = require (' path ');    var data = path.dirname ('/foo/strong/cool/nice '); Console.log (data);
Operation Result:
        '/foo/strong/cool '

Look at the above instructions and examples, how to feel, try it!

The DirName function obtains the corresponding directory name by passing in a string that needs to parse the path as a parameter.

5. Basic usage of basename function

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 >;

var path = require (' path ');    var data1 = path.basename ('/foo/strong/basename/index.html '); var data2 = Path.basename ('/foo/strong/basename/ Index.html ', '. html '); Console.log (Data1 + ' "and" ' + data2);
Operation Result:
        ' index.html ' and ' index '

After reading the two examples of the introduction and the difference, quickly hands-on feel it!

This function contains a must parameter, that is, a path string, and an optional parameter for the extension, absorbing a bit to continue efforts!

6. Basic usage of 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:

var path = require (' path ');    var data = path.extname (' index.html '); console.log (data);
Operation Result:
        '. html '

After a few hours of learning, I believe you have a few methods of path to fully understand the master, the strike, try Extname function bar!

When we need to do different operations according to different file extensions, this method is very useful!

7. Summary of Courses

I'm glad you've finished this lesson, here are some common ways of path, and if you want to know more, you can refer to the following address:

Http://nodejs.cn/api/pathhttp://nodeapi.ucdok.com/#/api/path.html

node. JS Learning Day fifth-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.