The path module contains a set of tools for processing and converting file paths, which can be accessed via require(‘path’). For Windows, directories are delimited with ”, for UNIX, directories are delimited with ‘/’, for ‘.. If multiple slashes or backslashes are found, they are replaced with one, /, //, \ and \ are uniformly converted to \

methods

1, path.resolve([path1], [path2]…)

If the merged paths do not form an absolute path, the absolute path of the current working directory is used by default.

Var _path = path.resolve('path3'.'path4'.'a/b\\cc\\'); // The result is: D: others\node\examples\path3\path4\a\b\cc // The concatenation path has an absolute path var _path = path.resolve('D://work/ready/demo'.'path3'.'path4'.'a/b\\cc\\'); // The result is: D: work\ready\demo\path3\path4\a\b\ccCopy the code

Note: D:\others\node\examples\ is my working path, please replace your own path to check

2、path.join([path1], [path2], …)

Paths are spliced and normalized in sequence. The path ends with a path separator.

  var _path = path.join('path1'.'path2\\p/cc//'.'/c/.. /ttt\\'); // The result is: path1\path2\p\cc\ TTT \Copy the code
3, path. Relative ()

Gets the relative path. Gets the relative relationship between two paths.

  • From The current path, and the method return value is based on the relative path specified from to, with no path separator at the end of the path
  • To which path
  var from = 'D:\\work\\server\\',
      to = 'D:/work/ready/demo'; var _path = path.relative(from, to); // Indicates the relative path from from to // the result is:.. \ready\demoCopy the code
4, path. IsAbsolute ()

Gets an absolute path, which is either an absolute path (such as ‘E:/ ABC ‘or ‘E:\’), or a path starting with a’/’ or ‘\’, both of which return true.

  console.log(path.isAbsolute('D:\\work\\server\\')); // true
  console.log(path.isAbsolute('\\work\\server')) / /true
  console.log(path.isAbsolute('/temp/.. /.. ')); // true
  console.log(path.isAbsolute('E:/github/nodeAPI/abc/efg')); // true
  console.log(path.isAbsolute('///temp123')); // true
  console.log(path.isAbsolute('//temp123')); // true
Copy the code
5, the path. The normalize (path)

The path is parsed and normalized.

  var _path = path.normalize(__dirname + '//test/a//b/.. //c\\'); // Result: D: others\node\examples\test\a\c\
Copy the code
6, the path. The format (pathObject)

This method is used to turn a string-type path into a pathObject (pathObject). The path object includes the file directory, filename, extension, and so on.

  var _path = path.format(path.format({ root: 'E:\\',
    dir: 'D:\\others\\node\\examples/test',
    base: 'util you.mp4',
    ext: '.mp4',
    name: 'util you as me'})); // Result: D:\others\node\examples/test\util you.mp4
Copy the code
7, the path. The parse ()

This method is used to convert a path object to a string type path (pathString).

  var _path = path.parse(__dirname + '/test/util you.mp3'); // result: // {root:'D:\\',
  //   dir: 'D:\\others\\node\\examples/test',
  //   base: 'util you.mp3',
  //   ext: '.mp3',
  //   name: 'util you' 
  // }
Copy the code
8, the path. The dirname ()

Gets the directory structure in the path, not the result path after normalize.

  var _path = path.dirname(__dirname + '/test/hha/util you.mp3'); // Note that the result is: D:\others\node\examples/test/hha 
  var _path = path.dirname(__dirname + '/test/hha/'); // Note that the result is: D:\others\node\examples/test
Copy the code
9, the path. The basename ()

Get the file name in the path (.ext includes the suffix).

var _path = path.basename(__dirname + '/test/util you.mp3'.'.mp3');
var _path = path.basename(__dirname + '/test/util you'.'.mp3');
var _path = path.basename(__dirname + '/test/util you'.'.ext'); // Util you var _path = path.basename(__dirname +'/test/util you.mp3');
var _path = path.basename(__dirname + '/test/util you.mp3'.' ');
var _path = path.basename(__dirname + '/test/util you.mp3'.'.ext'); // util you.mp3 var _path = path.basename(__dirname +'/test/'.'.mp3'); // The result is:test
Copy the code
10 and path. Extname ()

Gets the file name extension in the path.

  var _path = path.extname(__dirname + '/test/util you.mp3'); // The result is.mp3 var _path = path.extname(__dirname +)'/test/util you'); // The result is: emptyCopy the code

attribute

Path. win32 and path.posix

Both of these are attributes. Path: The system determines whether to operate a path in Windows or Linux mode based on the current operating system. Path. win32: Allows you to manipulate paths in Windows mode on any operating system. Path. POSIx: Allows you to manipulate paths using Linux on any operating system. On Windows, path==path.win32. On Linux, path==path.posix.

  console.log(path == path.win32); // true
  console.log(path == path.posix); // false
Copy the code
2, the path delimiter

Returns the directory separator in the operating system, such as window is ‘; ‘, ‘:’ in Unix.

console.log(path.delimiter); // The result is:;Copy the code
3, path. Sep

Returns the file separator in the operating system; Window is ‘\’, Unix is ‘/’.

console.log(path.sep); // Result: \ console.log('foo\\bar\\baz'.split(path.sep)); / / /'foo'.'bar'.'baz' ]
  console.log('foo/bar/baz'.split(path.sep)); // win returns ['foo/bar/baz'], but under *nix system will return ['foo'.'bar'.'baz']
Copy the code

See official documentation