To read more articles in this series please visit myMaking a blog, sample code please visithere.

Path (path)

The path module is mainly used to process file paths, such as extracting paths, suffixes, and splicing paths.

The use of the path

Here are some examples to familiarize yourself with using path:

Code example: /lesson12/path.js

const path = require('path')

const str = '/root/a/b/1.txt'Log (path.dirname(STR)) // Obtain the file directory: /root/a/b console.log(path.basename(STR)) // Obtain the file name: TXT console.log(path.extname(STR)) // Obtain the file suffix:.txt console.log(path.resolve(STR,'.. /c'.'build'.'strict'// Resolve the path to an absolute path: C:\root\a\b\ C \build\strict console.log(path.resolve(STR,'.. /c'.'build'.'strict'.'.. /.. '.'assets'Resolve (__dirname, C:\root\a\b\ C \assets console.log(path.resolve(__dirname,'build'// Resolve path to absolute path: C: projects\nodejs-tutorial\lesson12\buildCopy the code

A notable example is the path.resolve method, which takes any parameter and resolves the path to an absolute path based on the relationship between each path parameter.

__dirname refers to the name of the absolute path where the current module is located. Its value automatically changes based on the current absolute path and is the result of path.dirname(__filename).