1. Common methods of path
a. Path module (path)
Encapsulates various path related operations
Like Buffer, the path in NodeJS is also a special module
The difference is that the Buffer module has been added to Global, Therefore, manual import is not required
The Path module was not added to Global, Therefore, manual import is required
b. Get the last part of the path
path.basename(path[, ext])
let path = require("path"); // basename is used to get the last component of the path // let res = path.basename('/a/b/c/d/index.html');//index.html // let res = path.basename('/a/b/c/d'); //d let res = path.basename('/a/b/c/d/index.html', ".html"); //index console.log(res);
c. Get path
path.dirname(path)
// dirname is used to get the directory in the path, that is, the content except the last part // let res = path.dirname('/a/b/c/d/index.html'); // /a/b/c/d let res = path.dirname('/a/b/c/d'); // /a/b/c console.log(res);
d. Get extension name
path.extname(path)
// extname is used to get the extension of the last component in the path // let res = path.extname('/a/b/c/d/index.html'); //.html let res = path.extname('/a/b/c/d'); // console.log(res);
e. Determine whether it is an absolute path
path.isAbsolute(path)
isAbsolute is used to determine whether the path is an absolute path
Note:
Differentiated operating system
In the Linux operating system, / starts with an absolute path
In the Windows operating system, the drive letter begins with an absolute path
In the Linux operating system, the path separator is a left slash /
In the Windows operating system, the path separator is a right slash \
// let res = path.isAbsolute('/a/b/c/d/index.html'); // true // let res = path.isAbsolute('./a/b/c/d/index.html'); // false // let res = path.isAbsolute('c:\\a\\b\\c\\d\\index.html'); // true let res = path.isAbsolute('a\\b\\c\\d\\index.html'); // false console.log(res);
f. Gets the current operating system path separator
path.delimiter (windows is)\ Linux (yes /)
// path.delimiter is used to obtain the delimiter of the current operating system environment variable // If you are running in the Linux operating system, you will get: // If it is running in the Windows operating system, the obtained is; console.log(path.delimiter); // ;
g. Gets the current path environment variable separator
path.sep is used to obtain the separator of the path in the current operating system
If you are running in the Linux operating system, what you get is Left slash /
If you are running in the Windows operating system, you will get Right slash \
path.sep (used in windows); (used in linux:)
console.log(path.sep); // \
2.path other methods
i. Path formatting:
path.parse(path): Used to convert paths to objects
let obj = path.parse("/a/b/c/d/index.html"); console.log(obj);
path.format(pathObject): Used to convert objects to paths
let obj = { root: '/', dir: '/a/b/c/d', base: 'index.html', ext: '.html', name: 'index' }; let str = path.format(obj); console.log(str);
ii. Splicing path:
path.join([...paths]): For splicing paths
Note:
- If / is not added to the parameter, Then the method is automatically added
- If there is, The path will be automatically generated according to the previous parameters, Go to the upper level path
// let str = path.join("/a/b", "c"); // /a/b/c // let str = path.join("/a/b", "/c"); // /a/b/c // let str = path.join("/a/b", "/c", "../"); // /a/b/c --> /a/b let str = path.join("/a/b", "/c", "../../"); // /a/b/c --> /a console.log(str);
iii. standardization path:
path.normalize(path): Used to normalize paths
let res = path.normalize("/a//b///cd/index.html"); console.log(res); // \a\b\c\d\index.html
iv. calculate relative path:
path.relative(from, to): Used to calculate relative paths
First parameter: / data/orandea/test/aaa
Second parameter: / data/orandea/impl/bbb
/** * /data/orandea/test/aaa --> ../ --> /data/orandea/test * /data/orandea/test --> ../ --> /data/orandea * ..\..\impl\bbb */ let res = path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb'); console.log(res); // ..\..\impl\bbb
v. Resolution path:
path.resolve([...paths]): Used to resolve paths
Note: If the following parameter is an absolute path, Then the previous parameters will be ignored
// let res = path.resolve('/foo/bar', './baz'); // /foo/bar/baz // let res = path.resolve('/foo/bar', '../baz'); // /foo/baz let res = path.resolve('/foo/bar', '/baz'); // /baz console.log(res);