node中path路径模块的API有哪些

本文小编为大家详细介绍“node中path路径模块的API有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“node中path路径模块的API有哪些”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

疏勒网站建设公司创新互联,疏勒网站设计制作,有大型网站制作公司丰富经验。已为疏勒近千家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的疏勒做网站的公司定做!

node中path路径模块的API有哪些

1.path路径模块初认识

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

2.path模块的API

2.1 path.join()

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

语法格式为

node中path路径模块的API有哪些

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

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

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

console.log(pathStr)

node中path路径模块的API有哪些

2.2 path.basename()

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

语法格式

node中path路径模块的API有哪些

  • 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路径模块的API有哪些

2.3 path.extname()

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

格式为

node中path路径模块的API有哪些

  • path 必选参数,表示一个路径的字符串

  • 返回: 返回得到的扩展名字符串

const path=require("path")

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

const ftext =path.extname(fpath)

console.log(ftext)

node中path路径模块的API有哪些

3.时钟案例实践

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

源代码:http://127.0.0.1:5500/node/day1/static/index.html

3.1实现步骤

1.创建两个正则表达式,分别用来匹配 ','')     //将匹配的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('','')     //将匹配的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,'')     .replace(scriptruler,'')     //将匹配的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路径模块的API有哪些

读到这里,这篇“node中path路径模块的API有哪些”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注创新互联行业资讯频道。


当前标题:node中path路径模块的API有哪些
当前网址:http://pwwzsj.com/article/jjeejp.html