Home  >  Article  >  Web Front-end  >  A brief analysis of node’s path module

A brief analysis of node’s path module

青灯夜游
青灯夜游forward
2023-02-17 14:35:302407browse

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.

A brief analysis of node’s path module

node’s path module

Foreword: Through this article you will understand node Some API
of the path built-in module can be viewed on the node official website if necessary. Of course practice is greater than theory
So I prepared a case for practice

1. First introduction to the path module

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.

2.path module API

2.1 path.join()

path.join() method, used to splice multiple path fragments into a complete path string

The syntax format is
A brief analysis of node’s path module

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

A brief analysis of node’s path module

##2.2 path.basename()

Use path.basename() method, The last part of the path can be obtained. This method is often used to obtain the file name in the
path

Syntax format


A brief analysis of node’s path module

path is a required parameter, a string representing a path
  • Optional parameter, representing the file extension
  • 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 node’s path module

2.3 path.extname()

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

The format is


A brief analysis of node’s path module

    path is a required parameter, a string representing a path
  • Return: Return 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 node’s path module

3. Clock case practice

Split the provided code (one file has html, css, and js at the same time)
Split into three files, namely index.html index.css index. js and store it in a prepared file


Source code Right click to view the source code

3.1 Implementation steps

1. Create two regular expressions to match

3.1.1 Steps 1 - Import the required modules and create regular expression
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")
    })
}
Finally The result is that the style is stripped out in the specified file

. However, since the initial index.html contains all the code, the location where
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!

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