本文小編為大家詳細介紹“node中path路徑模塊的API有哪些”,內容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“node中path路徑模塊的API有哪些”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
疏勒網站建設公司創(chuàng)新互聯(lián),疏勒網站設計制作,有大型網站制作公司豐富經驗。已為疏勒近千家提供企業(yè)網站建設服務。企業(yè)網站搭建\成都外貿網站制作要多少錢,請找那個售后服務好的疏勒做網站的公司定做!
path 模塊是 Node.js 官方提供的、用來處理路徑的模塊。它提供了一系列的方法和屬性,用來滿足用戶對路徑的處理需求。
path.join() 方法,用來將多個路徑片段拼接成一個完整的路徑字符串
語法格式為
…paths(string) 路徑片段的序列 ,就是你需要拼接的所有路徑系列
需要注意的是這個返回的值為string
//引入path模塊 const path=require("path") //書寫要拼接的路徑 const pathStr=path.join('/a','/b/c','../','./d','e') console.log(pathStr)
使用 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)
path.extname()用于獲取路徑中的文件擴展名
格式為
path 必選參數,表示一個路徑的字符串
返回: 返回得到的擴展名字符串
const path=require("path") const fpath='./a/b/c/d/index.html' const ftext =path.extname(fpath) console.log(ftext)
將所提供的代碼(一個文件同時擁有html,css,js)進行拆分
拆分成三個文件分別為index.html index.css index.js并將其存放到一個準備好的文件中
源代碼:http://127.0.0.1:5500/node/day1/static/index.html
1.創(chuàng)建兩個正則表達式,分別用來匹配
<style>
和<script>
標簽
2. 使用 fs 模塊,讀取需要被處理的 HTML 文件
3. 自定義 resolveCSS 方法,來寫入 index.css 樣式文件
4. 自定義 resolveJS 方法,來寫入 index.js 腳本文件
5.自定義 resolveHTML 方法,來寫入 index.html 文件
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) })
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路徑模塊的API有哪些”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網站欄目:node中path路徑模塊的API有哪些
新聞來源:http://jinyejixie.com/article28/jjeejp.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供網頁設計公司、品牌網站設計、搜索引擎優(yōu)化、網站導航、網站收錄、網站建設
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)