The path module is a module officially provided by Node.js for processing paths . It provides a series of methods and attributes to meet users' needs for path processing.
path.join() method is 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.
It should be noted that the returned value is string
//Introduce the path module const path=require("path") //Write the path to be spliced const pathStr=path.join('/a','/b/c','../','./d','e') console.log(pathStr)
Use the path.basename() method to obtain the last part of the path. This method is often used to obtain
the syntax format
of the file name in the path.const path=require("path") const fpath='./a/b/c/index.html' var fullname=path.basename(fpath) console.log(fullname) //Get the file name with the specified suffix const namepath=path.basename(fpath,'.html') console.log(namepath)
path.extname() is used to obtain the file extension in the path.
The format is
path is a required parameter, a string representing a path.
Return: Returns the obtained extension string
const path=require("path") const fpath='./a/b/c/d/index.html' const ftext =path.extname(fpath) console.log(ftext)
split the provided code (one file has html, css, and js at the same time) into three files, namely index.html index.css index.js and store them in a prepared
Source code
in the file
: http://127.0.0.1:5500/node/day1/static/index.html1. Create two regular expressions to match
<style>
and<script>
tags respectively
2. Use the fs module to read the HTML file that needs to be processed
3. Customize the resolveCSS method to write the index.css style file
4. Customize the resolveJS method to write the index.js script file
5. Customize the resolveHTML method to write the index.html file
const path=require('path') const fs=require('fs') const regStyle=/<style>[sS]*</style>/ const scriptruler=/<script>[sS]*</script>/ //The file to be read fs.readFile(path.join(__dirname,'/static/index.html'),'utf-8',function(err,dateStr){ if(err){ return console.log("Read failed") } resolveCSS(dateStr) resolveHTML(dateStr) resolveJS(dateStr) })
function resolveCSS(htmlStr){ const r1=regStyle.exec(htmlStr) const newcss=r1[0].replace('<style>','').replace('</style>','') //Write the matching css into the specified index.css file fs.writeFile(path.join(__dirname,'/static/index.css'),newcss,function(err){ if(err) return console.log("Import failed"+err.message) console.log("ojbk") }) } function resolveJS(htmlStr){ const r2=scriptruler.exec(htmlStr) const newcss=r2[0].replace('<script>','').replace('</script>','') //Write the matching css into the specified index.js file fs.writeFile(path.join(__dirname,'/static/index.js'),newcss,function(err){ if(err) return console.log("Import failed"+err.message) console.log("ojbk") }) } function resolveHTML(htmlStr){ const newhtml=htmlStr .replace(regStyle,'<link rel="stylesheet" href="./index.css">') .replace(scriptruler,'<script src="./index.js"></script>') //Write the matching css into the specified index.html file fs.writeFile(path.join(__dirname,'/static/index2.html'),newhtml,function(err){ if(err) return console.log("Import failed"+err.message) console.log("ojbk") }) }
The final result is to peel off the style in the specified file
. However, since the initial index.html contains all the code, the location where it is stored when the style is split is still the original, so the final index.html code constant