Related articles

  • P01: Node.js tutorial from a practical perspective
  • P02: Basic node usage
  • P03: Node built-in module PATH
  • P04: Buffer for nodeAPI
  • P05: Events of node built-in module
  • P06: Node built-in module FS (1)
  • P07: Node built-in module FS (2)
  • P08: Node implements static server ~ create project
  • P09: Node implements static server ~ Hello HTTP
  • P10: Node implements static server ~ static file or folder reading

Built-in module PATH

  • Module provides utilities for working with file paths and directory paths
  • The default action for the PATH module varies depending on the operating system on which the Node.js application is running. Specifically, when running on a Windows operating system, the PATH module will assume that windows-style paths are being used. Therefore, using path.basename() may produce different results on POSIX and Windows
    • The path.normalize() method normalizes a given path, resolving ‘.. ‘and ‘.’ fragments
      const {normalize} = require('path')
      console.log(normalize('. /.. / / ') // Given a noncanonical path // output canonical path.. \Copy the code
    • The path.join() method joins all the given path fragments together using platform-specific separators as delimiters, and then normalizes the generated path. (Dealt with the relationship between superiors and subordinates, etc.)
      const {join} = require('path')
      console.log(join('/user'.'aaa'.'.. / '// output \user\ console.log(join()'/user'.'aaa'.'.. / '.'bbb') // output \user\ BBBCopy the code
    • The path.resolve() method resolves a sequence of paths or path fragments into absolute paths.
      console.log(resolve('/') // Output the absolute path E:\ XXX \ XXXX \ APICopy the code
    • The path.basename() method returns the last part of path, similar to the Unix basename command. The trailing directory delimiter is ignored
    • The path.dirname() method returns the directory name of path, similar to the Unix dirname command. The trailing directory delimiter is ignored
    • The path.extName () method returns the extension of path from the last occurrence of the. (period) character to the end of the string at the last part of path
      const {basename, dirname,extname} = require('path')
      const fileName = 'user/saa/xzz.txt'
      console.log(basename(fileName)) // xzz.txt
      console.log(dirname(fileName)) // user/saa
      console.log(extname(fileName)) // .txt
      Copy the code
    • The path.parse() method returns an object whose attributes represent the important elements of path. The trailing directory delimiter is ignored
    • Path.format () is the opposite of path.parse()
      const {parse, format} = require('path')
      const url = '/user/data/aaa/b.js'
      const ret = parse(url)
      console.log(ret)
      //{ root: '/',
      //  dir: '/user/data/aaa',
      //  base: 'b.js',
      //  ext: '.js',
      //  name: 'b' }
      console.log(format(ret))
      // /user/data/aaa\b.js
      Copy the code
    • Provide platform-specific path fragment separators:
      On Windows, \. On POSIX, it is /.Copy the code
    • Provide platform-specific path delimiters:
      ; For Windows: For POSIXCopy the code
    • The path.win32 property provides access to the Windows-specific implementation of the Path method
    • The PATH. posix property provides access to the POSIX-specific implementation of the path method.
      <! Const {sep, delimiter, win32, posix} = require('path')
      console.log('sep:', sep)
      // sep: \
      console.log('delimiter:', delimiter)
      // delimiter: ;
      console.log('posix sep:', posix.sep)
      // posix sep: /
      console.log('posix delimiter:', posix.delimiter)
      // posix delimiter: :
      Copy the code

Special attention to

  • About path: _dirNAME _filename always returns the absolute path to the file
  • About path: process.cwd() always returns the path where the node command was executed (where node was called)
  • The path of the require method is always relative to the current file

close