Preface:
Before explaining, explain the knowledge points involved;
path module: a practical tool for processing paths to files and directories. Modules must be introduced before use;
(): Used to link the path and will automatically convert the delimiter "/" or "\" of the current system path;
(): It is also used for linking paths, but it is very different from the () method. Moreover, the () method itself comes with an absolute path parameter of to, and it will automatically convert the separator, which is much more convenient to use in some scenarios;
__dirname: is a global variable of node, obtains the complete directory name of the directory where the current file is located, and is used with path;
Without further ado, let’s start the demonstration directly. After the demonstration is completed, they will summarize their characteristics.
About the use of ()
grammar:([from ...], to)
Case 1:
Demonstrate when the "path" path is empty, the result is the absolute path where the current file is located, similar to __dirname;
// Introduce path moduleconst path = require('path'); // Print directly here(()); (('')); ((__dirname); // Output: E:\Berlin-Case\path// Output: E:\Berlin-Case\path// Output:E:\Berlin-Case\path
Case 2:
When the demo character starts with ./ or does not have a character, the result is that adding or adding ./ does not affect the splicing of the path;
// Introduce path moduleconst path = require('path'); // Print directly here(()); (('a')); (('a','b')); (('c','b','a')); (('./a')); (('a','./b')); (('./c','b','./a')); // Output: E:\Berlin-Case\path// Output: E:\Berlin-Case\path\a// Output: E:\Berlin-Case\path\a\b// Output: E:\Berlin-Case\path\c\b\a// Output: E:\Berlin-Case\path\a// Output: E:\Berlin-Case\path\a\b// Output:E:\Berlin-Case\path\c\b\a
Case 3:
The demonstration character starts with /. One of the characteristics of () is that when you encounter the "/" slash, you will jump directly to the root path of E disk (which disk is running on which disk). The principle here is the same as outputting cd / at the terminal, and it will also jump to the root path of E disk;
// Introduce path moduleconst path = require('path'); // Print directly here(()); (('/a')); (('/a','b','c')); (('a','/b','c')); (('c','b','/a')); // Output: E:\Berlin-Case\path// Output: E:\a// Output: E:\a\b\c// Output: E:\b\c// Output:E:\a
Case 4:
The demonstration character begins with.../, which means the previous layer. () will overwrite the next "path" path to be spliced;
// Introduce path moduleconst path = require('path'); // Print directly here(()); (('../')); (('../a')); (('../a','b')); (('a','../b')); (('c','b','../a')); (('../c','b','a')); // Output: E:\Berlin-Case\path// Output: E:\Berlin-Case\// Output: E:\Berlin-Case\a// Output: E:\Berlin-Case\a\b// Output: E:\Berlin-Case\path\b// Output: E:\Berlin-Case\path\c\a// Output:E:\Berlin-Case\c\b\a
Case 5:
Demo() with __dername variable
// Introduce path moduleconst path = require('path'); // Print directly here((__dirname,'a')); (('a','b',__dirname)); ((__dirname,'./a','b')); ((__dirname,'/a','b')); ((__dirname,'../a','b')); ((__dirname,'a','../b')); (('a','/b',__dirname)); // Output: E:\Berlin-Case\path\a// Output: E:\Berlin-Case\path// Output: E:\Berlin-Case\path\a\b// Output: E:\a\b// Output: E:\Berlin-Case\a\b// Output: E:\Berlin-Case\path\b// Output:E:\Berlin-Case\path
From the five examples of the case, it can be seen that the __dirname variable must be placed first, otherwise it will cover the 'path' path before it, including the slash' / ', and there is also a point to pay attention to. ' / ' cannot appear after __dirname, otherwise the previous path will also be covered;
Summarize:
The characteristics of ("./path") are as follows:
- Read the "path" path from right to left and start splicing, which comes with its own absolute path parameter "to";
- When the "path" path is empty, the absolute path where the current file is located will be directly obtained;
- When a character starts with ./ or has no characters, it is spliced normally, so no characters can be omitted;
- When a character starts with /, it will not be spliced into the previous path and will be spliced with the disk where it is located as the root path;
- When a character starts with ../, the next path to be spliced "path" will be overwritten, and then continue to splice to the left;
- When paired with __dirname, the first position needs to be placed and there is a conflict with ' / ';
About the use of ()
grammar:([path1][, path2][, ...])
Case 1:
Demonstrate when the "path" path is none or empty, the result is ". ". Only when __dirname is passed in can the absolute path be obtained.
// Introduce path moduleconst path = require('path'); // Print directly here(()); (('')); ((__dirname)); // Output:.// Output:.// Output:E:\Berlin-Case\path
Case 2:
The demonstration character starts with ./ or / and has no characters. The result is that adding or not adding does not affect the splicing of the path. At this time, you should find that it is different from (), because resolve() will only splice the "path" path you wrote, and will not use cd to operate like ();
// Introduce path moduleconst path = require('path'); // Print directly here(()); (('a')); (('a','b')); (('c','b','a')); (('./a')); (('a','./b')); (('./c','b','./a')); (('/a')); (('a','/b')); (('/c','b','/a')); // Output:.// Output: a// Output: a\b// Output: c\b\a// Output: a// Output: a\b// Output: c\b\a// Output: \a// Output: a\b// Output:\c\b\a
Case Three:
Demonstrate characters starting with ../. At this time, you will find that join() is not only a simple splicing path, but also a splicing from right to left. If there is a "path" path after ../, it will also be overwritten;
// Introduce path moduleconst path = require('path'); // Print directly here(()); (('../')); (('../a')); (('../a','b')); (('a','../b')); (('c','b','../a')); (('../c','b','a')); // Output:.// Output:..\// Output:..\a// Output: ..\a\b// Output: b// Output: c\a// Output:..\c\b\a
Case 4:
Demo() is combined with the __dername variable, why do you have to put it first?
// Introduce path moduleconst path = require('path'); // Print directly here((__dirname,'a')); (('a',__dirname)); // Output: E:\Berlin-Case\path\a// Output:a\E:\Berlin-Case\path
Have you seen the difference? Right, that's right, join will splice the paths you write into one piece regardless of right or wrong, which is why it is put first. Of course, there is no such problem with resolve(). Let's continue to demonstrate and match characters;
// Introduce path moduleconst path = require('path'); // Print directly here((__dirname,'/a')); ((__dirname,'./a')); ((__dirname,'../')); ((__dirname,'../a')); ((__dirname,'../a','b')); // Output: E:\Berlin-Case\path\a// Output: E:\Berlin-Case\path\a// Output: E:\Berlin-Case\// Output: E:\Berlin-Case\a// Output:E:\Berlin-Case\a\b
As can be seen from the above example, the two characters '/' './' do not work in the() method, and the same effect is the same if they do not add. Only '../' can return to the previous directory. Therefore, when using(), add a __dirname and add the "path" path you want; (It will be very troublesome if you do not add)
Summarize:
- In the () method, '/' and './' can be used normally (except for splicing in special cases);
- In the () method, it is best to use it with the __dirname variable;
- () method is also parsed from right to left and arranged to form a path;
The difference between
Combined with the summary after the demonstration of the above two methods, the differences between them are as follows:
- () comes with to parameter, that is, the path of the current output file, but () does not;
- () encounters ' / ' and will jump to the root directory (E:\), while () will have no effect;
- When used with the __dirname variable, even if __dirname is on the rightmost, resolve() will overwrite the "path" path on the left to form the correct path, and () is spliced normally, regardless of whether it is right or wrong;
This is the end of this article about the difference and function of Zhonghe. For more related content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!