Home >Web Front-end >JS Tutorial >A brief analysis of some APIs of the path module in node

A brief analysis of some APIs of the path module in node

青灯夜游
青灯夜游forward
2022-05-26 13:00:121996browse

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!

A brief analysis of some APIs of the path module in node

1. First introduction to path module

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.

2.path module API

2.1 path.join()

path.join() method, used to combine multiple path fragments Spliced ​​into a complete path string

The syntax format is

A brief analysis of some APIs of the path module in node

...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)

A brief analysis of some APIs of the path module in node

##2.2 path. basename()

Use the path.basename() method to get the last part of the path. This method is often used to get the file name in the

path

Syntax format

A brief analysis of some APIs of the path module in node

    path required parameter, a string representing a path
  • Optional parameter, representing the file extension The name
  • represents the last part of the path
  • 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)

A brief analysis of some APIs of the path module in node

##2.3 path.extname()

path.extname() is used to get the file extension in the path

The format is

A brief analysis of some APIs of the path module in node

    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)

A brief analysis of some APIs of the path module in node

3. Clock case practice

Split the provided code (one file has html, css, and js at the same time)

Split into three files: index.html index.css index.js and store them in a prepared file


Source code:

http://127.0. 0.1:5500/node/day1/static/index.html

3.1 Implementation steps

1. Create two regular expressions, respectively Used to match
3.1.1 Step 1 - Import the required modules and create regular expressions

const path=require('path')
const fs=require('fs')

const regStyle=/<style>[\s\S]*<\/style>/

const scriptruler=/<script>[\s\S]*<\/script>/
//需要读取的文件
fs.readFile(path.join(__dirname,&#39;/static/index.html&#39;),&#39;utf-8&#39;,function(err,dateStr){
    if(err){
        return console.log("读取失败")
    }
   resolveCSS(dateStr)
   resolveHTML(dateStr)
   resolveJS (dateStr)
})

3.1.2 Custom resolveCSS resolveHTML resolveJS method

function resolveCSS(htmlStr){
    const r1=regStyle.exec(htmlStr)
    const newcss=r1[0].replace(&#39;<style>&#39;,&#39;&#39;).replace(&#39;</style>&#39;,&#39;&#39;)
    //将匹配的css写入到指定的index.css文件中
    fs.writeFile(path.join(__dirname,&#39;/static/index.css&#39;),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(&#39;<script>&#39;,&#39;&#39;).replace(&#39;</script>&#39;,&#39;&#39;)
    //将匹配的css写入到指定的index.js文件中
    fs.writeFile(path.join(__dirname,&#39;/static/index.js&#39;),newcss,function(err){
        if(err) return console.log("导入失败"+err.message)
        console.log("ojbk")
    })
}
function  resolveHTML(htmlStr){
    const newhtml=htmlStr
    .replace(regStyle,&#39;<link rel="stylesheet" href="./index.css">&#39;)
    .replace(scriptruler,&#39;<script src="./index.js"></script>&#39;)
    //将匹配的css写入到指定的index.html文件中
    fs.writeFile(path.join(__dirname,&#39;/static/index2.html&#39;),newhtml,function(err){
        if(err) return console.log("导入失败"+err.message)
        console.log("ojbk")
    })
}
The final result is to separate the styles in the specified file

But the initial index.html contains all the code, and then
the styles are split. The storage location is still the original, so the final index.html code remains unchanged


A brief analysis of some APIs of the path module in nodeFor more node-related knowledge, please visit:

nodejs tutorial

!

The above is the detailed content of A brief analysis of some APIs of the path module in node. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete