
パス モジュールは、パスを処理するために Node.js によって公式に提供されるモジュールです。これは、パス処理に対するユーザーのニーズを満たす一連のメソッドと属性を提供します。
path.join() メソッドは、複数のパス フラグメントを結合して完全なパス文字列にするために使用されます。
構文形式は次のとおりです。

...paths(string) パス フラグメントのシーケンスは、結合する必要があるすべてのパス シリーズです。
戻り値は string であることに注意してください。
//パス モジュールを導入します const path=require("path")
//結合するパスを記述する const pathStr=path.join('/a','/b/c','../','./d','e')
console.log(pathStr) 
パス内のファイル名のpath.basename() メソッドを使用してパスの最後の部分を取得します。このメソッドは、
構文形式
を取得するためによく使用されます。
path
const fpath='./a/b/c/index.html' var fullname=パス.ベース名(fpath) console.log(フルネーム) //指定されたサフィックスを持つファイル名を取得 const namepath=path.basename(fpath,'.html') console.log(名前パス)

path.extname() は、パス内のファイル拡張子を取得するために使用されます。
形式は次のとおりです。

path は必須パラメータであり、パスを表す文字列です。
戻り値: 取得した拡張文字列
const path=require("path")を返します。const fpath='./a/b/c/d/index.html' const ftext =path.extname(fpath) コンソール.ログ(ftext)

提供されたコード (1 つのファイルに html、css、および js が同時に含まれています) を 3 つのファイル (index.html、index.css、index.js) に分割し、用意された
に格納します。ファイル: http://127.0.0.1:5500/node/day1/static/index.htmlソース コード
1.
<style>と<script>タグにそれぞれ一致する 2 つの正規表現を作成します。
2. fs モジュールを使用して、処理する必要がある HTML ファイルを読み取ります。
3.solveCSS メソッドをカスタマイズして、index.css スタイル ファイルを作成します。
4.resolveJS メソッドをカスタマイズして、index.js スクリプト ファイルを作成します。
5.solveHTML メソッドをカスタマイズして、index.html ファイルを作成します。
const path=require('path')を作成します。
const fs=require('fs')
const regStyle=/<スタイル>[sS]*</スタイル>/
const scriptruler=/<script>[sS]*</script>/
//読み込むファイル fs.readFile(path.join(__dirname,'/static/index.html'),'utf-8',function(err,dateStr){
if(エラー){
return console.log("読み取りに失敗しました")
}
解決CSS(dateStr)
解決HTML(dateStr)
solveJS(dateStr)
}) 関数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")
})
}
関数solveJS(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")
})
}
関数solveHTML(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はコード定数
