软件开发定制定制关于node的path路径模块

文章目录


的path模块

☀️作者简介:软件开发定制定制大家好我是言不及行yyds
🐋个人主页:言不及行yyds的CSDN博客
🎁系列专栏:【node】

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

1.path路径模块初认识

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

2.path模块的API

2.1 path.join()

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

语法格式为

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

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

//引入path模块const path=require("path")//书写要拼接的路径const pathStr=path.join('/a','/b/c','../','./d','e')console.log(pathStr)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.2 path.basename()

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

语法格式

  • 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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2.3 path.extname()

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

格式为

  1. path 必选参数,表示一个路径的字符串
  2. 返回: 返回得到的扩展名字符串
const path=require("path")const fpath='./a/b/c/d/index.html'const ftext =path.extname(fpath)console.log(ftext)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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)})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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")    })}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

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

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

好了这次的文章就到这了
如果觉得还不错的话,帮忙点个关注
欢迎大家积极探讨

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发