The path module is an official module provided by Node.js to process paths. It provides a series of methods and attributes to meet the user's processing requirements for paths
For example:
The path. join() method is used to splice multiple path fragments into a complete path string
The path. basename() method is used to parse the file name from the path string
If you want to use the path module to process the path in JavaScript, you need to import it first using the following method
const path=require('path')
1. Path. join() syntax format
Using the path. join() method, multiple paths can be spliced into a complete path string. The syntax format is as follows:
path.join([...paths])
Parameter interpretation:
... paths path fragment sequence
Return value: concatenated string
const pathStr=path.join('/a','/b/c','../','./d','e')
console.log(pathStr) // \a\b\d\e
2. Syntax format of path. basename()
The path. basename() method can be used to obtain the last part of the path. This method is often used to obtain the file name in the path. The syntax format is as follows:
const fpath='a/b/c/index.html'
const fullName=path.basename(fpath)
console.log(fullName) // index.html
3. Syntax format of path. extname()
Use the path. extname() method to obtain the extension part of the path. The syntax format is as follows:
path.extname(path)
Parameter interpretation:
Path: path string, required parameter
Return: get the extension string of the file
const fext=path.extname(fpath)
console.log(fext) // .html