node的path路径模块怎么使用

发布时间:2023-02-17 16:27:16 作者:iii
来源:亿速云 阅读:111

这篇文章主要介绍了node的path路径模块怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇node的path路径模块怎么使用文章都会有所收获,下面我们一起来看看吧。

node的path模块

前言:通过这篇文章你会了解node的path内置模块的一些API
如果需要的话可到node官网查看。当然实践大于理论
所以我准备了一个案例,用于练手

1.path路径模块初认识

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

2.path模块的API

2.1 path.join()

path.join() 方法,用来将多个路径片段拼接成一个完整的路径字符串

语法格式为
node的path路径模块怎么使用

…paths(string) 路径片段的序列 ,就是你需要拼接的所有路径系列。

需要注意的是这个返回的值为string

//引入path模块
const path=require("path")
//书写要拼接的路径
const pathStr=path.join('/a','/b/c','../','./d','e')

console.log(pathStr)

node的path路径模块怎么使用

2.2 path.basename()

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

语法格式
node的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)

node的path路径模块怎么使用

2.3 path.extname()

path.extname()用于获取路径中的文件扩展名

格式为
node的path路径模块怎么使用

const path=require("path")

const fpath='./a/b/c/d/index.html'

const ftext =path.extname(fpath)

console.log(ftext)

node的path路径模块怎么使用

3.时钟案例实践

将所提供的代码(一个文件同时拥有html,css,js)进行拆分
拆分成三个文件分别为index.html index.css index.js并将其存放到一个准备好的文件中

源代码
点击右键查看源代码

3.1实现步骤

1.创建两个正则表达式,分别用来匹配 <style><script> 标签
2. 使用 fs 模块,读取需要被处理的 HTML 文件
3. 自定义 resolveCSS 方法,来写入 index.css 样式文件
4. 自定义 resolveJS 方法,来写入 index.js 脚本文件
5.自定义 resolveHTML 方法,来写入 index.html 文件

3.1.1步骤1 - 导入需要的模块并创建正则表达式

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,'/static/index.html'),'utf-8',function(err,dateStr){
    if(err){
        return console.log("读取失败")
    }
   resolveCSS(dateStr)
   resolveHTML(dateStr)
   resolveJS (dateStr)
})

3.1.2 自定义 resolveCSS resolveHTML resolveJS 方法

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

最终的结果就是在指定的文件中将样式剥离开

但是那个最开始的index.html由于是包含全部的代码,而后
在拆分样式的时候存放的位置还是原来的,所以最终index.html的代码不变

node的path路径模块怎么使用

关于“node的path路径模块怎么使用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“node的path路径模块怎么使用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 如何在node.js中使用path路径模块
  2. PATH模块怎么在node.JS中使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

node path

上一篇:linux能不能用鼠标

下一篇:Go语言中资源竞争问题怎么解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》