Nodejs, the distinction path.join () and path.resolve () of

  • Talking path.join()and path.resolve()before a difference, let me say at the file path /and ./and ../the difference between

    • /It represents the root directory;

    • ./It represents the current directory;

    • ../It represents the parent directory.

Then it down path.join()and path.resolve()differentiated

We need to note that these two methods belong to paththe module, we need to use before the introduction module

const path = require('path');

Enter text:

path.resolve()

  • Methods role
    • path.resolveThe method of the incoming path or path segment that resolves to an absolute path.
  • grammar:
path.resolve([from ...], to)
  • Description:
    • If no incoming path segment, or segment length path is zero (null character), then path.resolve()returns the absolute path of the current working directory (using equivalent path.resolve(__dirname))
  • Borrow someone else's code, we look at a classic example, suppose the current working directory/workspace/demo
console.log(path.resolve())      // returns /workspace/demo
console.log(path.resolve(''))     // returns /workspace/demo
console.log(path.resolve(__dirname)) // returns /workspace/demo
console.log(path.resolve('/img/books', '/net'))  // returns '/net'
console.log(path.resolve('img/books', '/net'))  // returns '/net'
console.log(path.resolve('img/books', './net'))  // returns '/workspace/demo/img/books/net'
console.log(path.resolve('/img/books', './net'))  // returns '/img/books/net'
console.log(path.resolve('/img/books', 'net'))   // returns '/img/books/net'
console.log(path.resolve('/img/books', '../net'))     // returns '/img/net'
console.log(path.resolve('src','/img/books', '../net'))  // returns '/img/net'
console.log(path.resolve('src','./img/books', '../net'))  // returns '/workspace/demo/src/img/net'
console.log(path.resolve('src','img/books', '../net'))   // returns '/workspace/demo/src/img/net'
  • Description: We said at the beginning of the article ./and ../and /will come in handy distinction, from right to left, we can see if the path to /begin with, it will not stitching path to the left, as it has a root directory If it is ./the beginning or do not sign, it will splice the path to the left, if by ../the beginning of stitching on the left of the path of the parent path.

path.join()

  • Methods Role:
    • path.join () method to a platform-specific delimiter entire given path segments connected together, and the standardization of the generated paths.
  • grammar:
path.join([path1], [path2], [...])
  • Description:
    • Zero-length pathsegments are ignored. If the path is connected to a string of zero length string is returned ., indicating the current working directory
  • Borrow someone else's code, we look at a classic example
path.join('/img', 'book', 'net/abc', 'inter', '..'); // returns /img/book/net/abc
console.log(path.join('/img/books', '../net'))  // returns /img/net
console.log(path.join('img/books', '../net'))   // returns img/net
console.log(path.join('/img/books', './net'))   // returns /img/books/net
console.log(path.join('img/books', './net'))   // returns img/books/net
console.log(path.join('/img/books', 'net'))    // returns /img/books/net
console.log(path.join('img/books', 'net'))    // returns /img/books/net
console.log(path.join('/img/books', '/net'))   // returns /img/books/net
console.log(path.join('img/books', '/net'))    // returns img/books/net

Guess you like

Origin www.cnblogs.com/nayek/p/12165359.html