The path module is a built-in module in nodejs for processing file/directory paths. It can be regarded as a toolbox, providing many methods for us to use, of course all related to path processing. At the same time, the path module appears frequently in front-end development, such as when configuring webpack. This article will talk about the path module of node.
Foreword: Through this article you will understand node Some API
of the path built-in module can be viewed on thenode official websiteif necessary. Of course practice is greater than theory
So I prepared a case for practice
The path module is a moduleofficially provided by Node.js forprocessing paths. It provides a series of methods and attributes to meet users' needs for path processing.
path.join() method, used to splice multiple path fragments into a complete path string
The syntax format is
…paths(string) The sequence of path fragments is all the path series you need to splice. [Related tutorial recommendations:nodejs video tutorial,Programming teaching]
##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)
path Syntax format
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)
Split into three files, namely index.html index.css index. js and store it in a prepared file
Source codeRight click to view the source code
','') //将匹配的css写入到指定的index.css文件中 fs.writeFile(path.join(__dirname,'/static/index.css'),newcss,function(err){ if(err) return console.log("导入失败"+err.message) console.log("ojbk") }) } function resolveJS(htmlStr){ const r2=scriptruler.exec(htmlStr) const newcss=r2[0].replace('','') //将匹配的css写入到指定的index.js文件中 fs.writeFile(path.join(__dirname,'/static/index.js'),newcss,function(err){ if(err) return console.log("导入失败"+err.message) console.log("ojbk") }) } function resolveHTML(htmlStr){ const newhtml=htmlStr .replace(regStyle,'') .replace(scriptruler,'') //将匹配的css写入到指定的index.html文件中 fs.writeFile(path.join(__dirname,'/static/index2.html'),newhtml,function(err){ if(err) return console.log("导入失败"+err.message) console.log("ojbk") }) }Copy after login
is stored when the style is split is still the same. Original, so the code of final index.html remains unchanged
For more node-related knowledge, please visit: nodejs tutorial!
The above is the detailed content of A brief analysis of node's path module. For more information, please follow other related articles on the PHP Chinese website!