人妻精品在线观看一区二区三区,蜜臀av精品一区二区三区网站,中文一区二区三区亚洲欧美,熟女人妇精品一区二区,人妻av在线观看视频,欧美日韩国产三级精品网站,黄色免费网站直接进入,超碰公开福利正在播放,国产毛片乡下农村妇女毛片

淺析node的path路徑模塊-熱消息

來(lái)源:php中文網(wǎng) | 2023-02-18 11:57:28 |

path 模塊是 nodejs 中用于處理文件/目錄路徑的一個(gè)內(nèi)置模塊,可以看作是一個(gè)工具箱,提供諸多方法供我們使用,當(dāng)然都是和路徑處理有關(guān)的。同時(shí)在前端開發(fā)中 path 模塊出現(xiàn)的頻率也是比較高的,比如配置 webpack 的時(shí)候等。本文就來(lái)聊聊node的path路徑模塊。

node的path模塊

1.path路徑模塊初認(rèn)識(shí)

path 模塊是 Node.js 官方提供的、用來(lái)處理路徑的模塊。它提供了一系列的方法和屬性,用來(lái)滿足用戶對(duì)路徑的處理需求。


(資料圖)

2.path模塊的API

2.1 path.join()

path.join() 方法,用來(lái)將多個(gè)路徑片段拼接成一個(gè)完整的路徑字符串

語(yǔ)法格式為

…paths(string) 路徑片段的序列 ,就是你需要拼接的所有路徑系列?!鞠嚓P(guān)教程推薦:nodejs視頻教程、編程教學(xué)】

需要注意的是這個(gè)返回的值為string

//引入path模塊const path=require("path")//書寫要拼接的路徑const pathStr=path.join("/a","/b/c","../","./d","e")console.log(pathStr)

2.2 path.basename()

使用 path.basename() 方法,可以獲取路徑中的最后一部分,經(jīng)常通過(guò)這個(gè)方法獲取路徑中的文件名

語(yǔ)法格式

path 必選參數(shù),表示一個(gè)路徑的字符串可選參數(shù),表示文件擴(kuò)展名表示路徑中的最后一部分
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)

2.3 path.extname()

path.extname()用于獲取路徑中的文件擴(kuò)展名

格式為

path 必選參數(shù),表示一個(gè)路徑的字符串

返回: 返回得到的擴(kuò)展名字符串

const path=require("path")const fpath="./a/b/c/d/index.html"const ftext =path.extname(fpath)console.log(ftext)

3.時(shí)鐘案例實(shí)踐

將所提供的代碼(一個(gè)文件同時(shí)擁有html,css,js)進(jìn)行拆分 拆分成三個(gè)文件分別為index.html index.css index.js并將其存放到一個(gè)準(zhǔn)備好的文件中

源代碼點(diǎn)擊右鍵查看源代碼

3.1實(shí)現(xiàn)步驟

1.創(chuàng)建兩個(gè)正則表達(dá)式,分別用來(lái)匹配 <style><script>標(biāo)簽 2. 使用 fs 模塊,讀取需要被處理的 HTML 文件 3. 自定義 resolveCSS 方法,來(lái)寫入 index.css 樣式文件 4. 自定義 resolveJS 方法,來(lái)寫入 index.js 腳本文件 5.自定義 resolveHTML 方法,來(lái)寫入 index.html 文件

3.1.1步驟1 - 導(dǎo)入需要的模塊并創(chuàng)建正則表達(dá)式

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("導(dǎo)入失敗"+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("導(dǎo)入失敗"+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("導(dǎo)入失敗"+err.message)        console.log("ojbk")    })}

最終的結(jié)果就是在指定的文件中將樣式剝離開

但是那個(gè)最開始的index.html由于是包含全部的代碼,而后 在拆分樣式的時(shí)候存放的位置還是原來(lái)的,所以最終index.html的代碼不變

更多node相關(guān)知識(shí),請(qǐng)?jiān)L問(wèn):nodejs 教程!

以上就是淺析node的path路徑模塊的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!

關(guān)鍵詞: node path模塊