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.