This article will introduce you to the path path module of node, introduce some APIs of the path built-in module, and also prepare a case for practice. I hope it will be helpful to everyone!
path module is officially provided by Node.js and is used for processing Path to module . It provides a series of methods and attributes to meet users' needs for path processing.
path.join() method, used to combine multiple path fragments Spliced into a complete path string
The syntax format is
...paths(string) The sequence of path fragments is you All path series that need to be spliced
It should be noted that the returned value is string
//引入path模块 const path=require("path") //书写要拼接的路径 const pathStr=path.join('/a','/b/c','../','./d','e') console.log(pathStr)
Use the path.basename() method to get the last part of the path. This method is often used to get the file name in theSyntax formatpath
const path=require("path") const fpath='./a/b/c/index.html' var fullname=path.basename(fpath) console.log(fullname) //获取指定后缀的文件名 const namepath=path.basename(fpath,'.html') console.log(namepath)
The format is
const path=require("path") const fpath='./a/b/c/d/index.html' const ftext =path.extname(fpath) console.log(ftext)
Source code:3.1 Implementation stepshttp://127.0. 0.1:5500/node/day1/static/index.html