亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    淺析node的path路徑模塊

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

    淺析node的path路徑模塊

    node的path模塊

    前言:通過這篇文章你會(huì)了解node的path內(nèi)置模塊的一些API
    如果需要的話可到node官網(wǎng)查看。當(dāng)然實(shí)踐大于理論
    所以我準(zhǔn)備了一個(gè)案例,用于練手

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

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

    2.path模塊的API

    2.1 path.join()

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

    語法格式為
    淺析node的path路徑模塊

    …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)
    登錄后復(fù)制

    淺析node的path路徑模塊

    2.2 path.basename()

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

    語法格式
    淺析node的path路徑模塊

    • 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)
    登錄后復(fù)制

    淺析node的path路徑模塊

    2.3 path.extname()

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

    格式為
    淺析node的path路徑模塊

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

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

    const path=require("path")  const fpath='./a/b/c/d/index.html'  const ftext =path.extname(fpath)  console.log(ftext)
    登錄后復(fù)制

    淺析node的path路徑模塊

    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á)式,分別用來匹配 <style><script> 標(biāo)簽
    2. 使用 fs 模塊,讀取需要被處理的 HTML 文件
    3. 自定義 resolveCSS 方法,來寫入 index.css 樣式文件
    4. 自定義 resolveJS 方法,來寫入 index.js 腳本文件
    5.自定義 resolveHTML 方法,來寫入 index.html 文件

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

    const path=require('path') const fs=require('fs')  const regStyle=/<style>[sS]*</style>/  const scriptruler=/<script>[sS]*</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) })
    登錄后復(fù)制

    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")     }) }
    登錄后復(fù)制

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

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

    淺析node的path路徑模塊

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)