nodejs学习-path模块

本文详细介绍了Node.js中的path模块,包括path.join()、path.basename()和path.extname()等方法的使用。通过实例展示了如何利用这些方法处理路径,以及在实际文件操作中的应用。同时,给出了一个提取HTML中CSS和JS文件的练习案例,演示了如何从HTML中提取样式和脚本并分别写入新的文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

path模块是nodejs官方提供的、用来处理路径的模块。它提供了一系列的方法和属性,用来满足用户对路径的处理需求。

例如:

path.join()方法,用来将多个路径片段拼接成一个完整的路径字符串
path.basename()方法,用来从路径字符串中,将文件名解析出来

如果要在JavaScript代码中使用path模块来操作文件,则需要使用如下的方式先导入它:

const path=require('path')

path.join()的语法格式

path.join([...paths])

参数解读:
...paths<string>路径片段的序列
返回值:<string>
使用path.join()方法,可以把多个路径片段拼接为完整的路径字符串:

const pathStr=path.join('/a','/b/c','../','./d','e')
console.log(pathStr) //输出 \a\b\d\e

const pathStr2=path.join(__dirname,'./files/1.txt')
console.log(pathStr2) //输出当前文件所处目录\files\1.txt

注意:今后凡是涉及到路径拼接的操作,都要使用path.join()方法进行处理。不要直接使用+进行字符串的拼接。

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

// ../会抵消前面的路径
const pathStr=path.join('/a','/b/c','../','./d','e')
console.log(pathStr)

fs.readFile(path.join(__dirname,'./files/1.txt'),'utf8',function(err,dataStr){
    if(err){
        return console.log(err.message)
    }
    console.log(dataStr)
})

path.basename()的语法格式

使用path.basename()方法,可以获取路径中的最后一部分,经常通过这个方法获取路径中的文件名,语法格式如下:

path.basename(path[,ext])

参数解读:

path<string>必选参数,表示一个路径的字符串
ext<string>可选参数,表示文件扩展名
返回:<string>表示路径中的最后一部分

const path=require('path')

// 定义文件的存放路径
const fpath='/a/b/c/index.html'

// const fullname=path.basename(fpath)
// console.log(fullname)    //index.html

const nameWithExt=path.basename(fpath,'.html')
console.log(nameWithExt)    //index

path.extname()语法格式

使用path.extname()方法,可以获取路径中的扩展名部分,语法格式如下:

path.extname(path)

参数解读:

path<string>必选参数,表示一个路径的字符串
返回:<string>返回得到的扩展名字符串

const path=require('path')

// 这是文件的存放路径
const fpath='/a/b/c/index.html'

const fext=path.extname(fpath)
console.log(fext)   //输出 .html

练习

HTML、css、js文件的提取

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

// 定义正则表达式,分别匹配<style></style>和<script></script>标签
const regStyle=/<style>[\s\S]*<\/style>/
const regScript=/<script>[\s\S]*<\/script>/

// 调用fs.readFile()方法读取文件
fs.readFile(path.join(__dirname,'./素材/index.html'),'utf8',function(err,dataStr){
    if(err) return console.log('读取HTML文件失败~'+err.dataStr)
    // 读取文件成功后,调用对应的三个方法,分别解析出HTML、css、js
    resolveCSS(dataStr)
    resolveJS(dataStr)
    resolveHtml(dataStr)
})

// 定义处理CSS样式的方法
function resolveCSS(htmlStr){
    // 使用正则提取需要的内容
    const r1=regStyle.exec(htmlStr)
    // console.log(r1)
    // 将提取出来的样式字符串,进行字符串的replace替换操作
    const newCSS=r1[0].replace('<style>','').replace('</style>','')
    // console.log(newCSS)
    // 调用fs.writeFile()方法,将提取的样式,写入到clock目录中index.css的文件里面
    fs.writeFile(path.join(__dirname,'./clock/index.css'),newCSS,function(err){
        if(err) return console.log('写入css样式失败~'+err.message)
        console.log('写入样式文件成功')
    })
}

// 定义处理JS脚本的方法
function resolveJS(htmlStr){
    // 通过正则,提取对应的<script>标签内容
    const r2=regScript.exec(htmlStr)
    const newJS=r2[0].replace('<script>','').replace('</script>','')
    fs.writeFile(path.join(__dirname,'./clock/index.js'),newJS,function(err){
        if(err) return console.log('写入js样式失败~'+err.message)
        console.log('写入js脚本成功')
    })
}

function resolveHtml(htmlStr){
    // 将字符串调用replace方法,把内嵌的style和script替换为外联的link和script标签
    const newHTML=htmlStr.replace(regStyle,'<link rel="stylesheet" href="./index.css" />').replace(regScript,'<script src="./index.js"></script>')
    fs.writeFile(path.join(__dirname,'./clock/index.html'),newHTML,function(err){
        if(err) return console.log('写入HTML文件失败'+err.message)
        console.log('写入HTML页面成功')
    })
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值