您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)PATH模塊怎么在node.JS中使用,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
返回路徑p所在的目錄
var path = require('path'); console.log(path.dirname('/foo/bar/baz/asdf/a.txt')); // /foo/bar/baz/asdf console.log(path.dirname('/foo/bar/baz/asdf/')); // /foo/bar/baz console.log(path.dirname('C:/test/aaa')); // C:/test
返回路徑的最后一個(gè)部分,即文件名。參數(shù)ext為需要截掉的后綴內(nèi)容
var path = require('path'); console.log(path.basename('/foo/bar/baz/asdf/a.txt')); // a.txt console.log(path.basename('/foo/bar/baz/asdf/a.txt','.txt')); // a console.log(path.basename('/foo/bar/baz/asdf/')); // asdf console.log(path.basename('C:/test/aaa')); // aaa
返回路徑p的擴(kuò)展名,從最后一個(gè)'.'到字符串的末尾。如果最后一個(gè)部分沒有'.',或者路徑是以'.'開頭,則返回空字符串
var path = require('path'); console.log(path.extname('/foo/bar/baz/asdf/a.txt')); // .txt console.log(path.extname('/foo/bar/baz/asdf/a.txt.b')); // .b console.log(path.extname('/foo/bar/baz/asdf/a.')); // . console.log(path.extname('C:/test/aaa/.')); // '' console.log(path.extname('C:/test/aaa')); // ''
返回對應(yīng)平臺下的文件分隔符,win下為'\',*nix下為'/'
var path = require('path'); console.log(path.sep); // win下為\,*nix下為/ console.log('foo\\bar\\baz'.split(path.sep)); // [ 'foo', 'bar', 'baz' ] console.log('foo/bar/baz'.split(path.sep)); // win下返回['foo/bar/baz'],但在*nix系統(tǒng)下會返回['foo','bar','baz']
返回對應(yīng)平臺下的路徑分隔符,win下為';',*nix下為':'
var path = require('path'); console.log(path.delimiter); //win下為“;”,*nix下為“:” console.log(path.sep); // win下為\,*nix下為/
規(guī)范化路徑,處理冗余的“..”、“.”、“/”字符。發(fā)現(xiàn)多個(gè)斜杠時(shí),會替換成一個(gè)斜杠。當(dāng)路徑末尾包含一個(gè)斜杠時(shí),保留。Windows系統(tǒng)使用反斜杠
var path = require('path'); console.log(path.normalize('a/b/c/../user/bin'));//a\b\user\bin console.log(path.normalize('a/b/c///../user/bin/'));//a\b\user\bin\ console.log(path.normalize('a/b/c/../../user/bin'));//a\user\bin console.log(path.normalize('a/b/c/.././///../user/bin/..'));//a\user console.log(path.normalize('a/b/c/../../user/bin/../../'));//a\ console.log(path.normalize('a/../../user/bin/../../'));//..\ console.log(path.normalize('a/../../user/bin/../../../../'));//..\..\..\ console.log(path.normalize('./a/.././user/bin/./'));//user\bin\
將多個(gè)路徑結(jié)合在一起,并轉(zhuǎn)換為規(guī)范化路徑
var path = require('path'); console.log(path.join('////./a', 'b////c', 'user/'));//\a\b\c\user console.log(path.join('a', '../../', 'user/'));//..\user\
從源地址 from 到目的地址 to 的絕對路徑,類似在shell里執(zhí)行一系列的cd命令
path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')
類似于:
cd foo/bar cd /tmp/file/ cd .. cd a/../subfile pwd
[注意]如果某個(gè)from或to參數(shù)是絕對路徑(比如 'E:/abc',或是以“/”開頭的路徑),則將忽略之前的from參數(shù)
var path = require('path'); console.log(path.resolve('.', 'testFiles/..', 'trdLayer'));//D:\project\trdLayer console.log(path.resolve('..', 'testFiles', 'a.txt'));//D:\testFiles\a.txt console.log(path.resolve('D:', 'abc', 'D:/a'));//D:\a console.log(path.resolve('abc', 'ok.gif'));//D:\project\abc\ok.gif console.log(path.resolve('abc', '..', 'a/../subfile')); //D:\project\subfile
path是一個(gè)絕對路徑(比如 'E:/abc'),或者是以“/”開頭的路徑,二者都會返回true
var path = require('path'); console.log(path.isAbsolute('../testFiles/secLayer'));//false console.log(path.isAbsolute('./join.js'));//false console.log(path.isAbsolute('temp'));//false console.log(path.isAbsolute('/temp/../..'));//true console.log(path.isAbsolute('E:/github/nodeAPI/abc/efg'));//true console.log(path.isAbsolute('///temp123'));//true
獲取從 from 到 to 的相對路徑,可以看作 path.resolve 的相反實(shí)現(xiàn)
path.resolve(from, path.relative(from, to)) == path.resolve(to) var path = require('path'); console.log(path.relative('C:\\\test', 'C:\\\impl\\bbb'));//..\impl\bbb console.log(path.relative('C:/test/aaa', 'C:/bbb'));//..\..\bbb
上述就是小編為大家分享的PATH模塊怎么在node.JS中使用了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。