Nodejs基礎(chǔ)路徑處理模塊path總結(jié)_第1頁(yè)
Nodejs基礎(chǔ)路徑處理模塊path總結(jié)_第2頁(yè)
Nodejs基礎(chǔ)路徑處理模塊path總結(jié)_第3頁(yè)
Nodejs基礎(chǔ)路徑處理模塊path總結(jié)_第4頁(yè)
Nodejs基礎(chǔ)路徑處理模塊path總結(jié)_第5頁(yè)
已閱讀5頁(yè),還剩4頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Nodejs基礎(chǔ):路徑處理模塊path總結(jié)在nodejs中,path是個(gè)使用頻率很高,但卻讓人又愛(ài)又恨的模塊。部分因?yàn)槲臋n說(shuō)的不夠清晰,部分因?yàn)榻涌诘钠脚_(tái)差異性。作者:程序猿小卡_casper來(lái)源:segmentfault|2016-11-14 20:28 收藏   分享 模塊概覽在nodejs中,path是個(gè)使用頻率很高,但卻讓人又愛(ài)又恨的模塊。部分因?yàn)槲臋n說(shuō)的不夠清晰,部分因?yàn)榻涌诘钠脚_(tái)差異性。將path的接口按照用途歸類,仔細(xì)琢磨琢磨,也就沒(méi)那么費(fèi)解了。獲取路徑/文件名/擴(kuò)展名· 獲取路徑:path.dirname(filepath)·

2、獲取文件名:path.basename(filepath)· 獲取擴(kuò)展名:path.extname(filepath)獲取所在路徑例子如下:1. var path = require('path'); 2. var filepath = '/tmp/demo/js/test.js' 3.  4. / 輸出:/tmp/demo/js 5. console.log( path.dirname(filepath) ); 

3、 獲取文件名嚴(yán)格意義上來(lái)說(shuō),path.basename(filepath) 只是輸出路徑的最后一部分,并不會(huì)判斷是否文件名。但大部分時(shí)候,我們可以用它來(lái)作為簡(jiǎn)易的“獲取文件名“的方法。1. var path = require('path'); 2.  3. / 輸出:test.js 4. console.log( path.basename('/tmp/demo/js/test.js') ); 5.  6. / 輸出:test

4、60;7. console.log( path.basename('/tmp/demo/js/test/') ); 8.  9. / 輸出:test 10. console.log( path.basename('/tmp/demo/js/test') );  如果只想獲取文件名,單不包括文件擴(kuò)展呢?可以用上第二個(gè)參數(shù)。1. / 輸出:test 2. console.log( path.basename('/tmp/demo/

5、js/test.js', '.js') );  獲取文件擴(kuò)展名簡(jiǎn)單的例子如下:1. var path = require('path'); 2. var filepath = '/tmp/demo/js/test.js' 3.  4. / 輸出:.js 5. console.log( path.extname(filepath) );  更詳細(xì)的規(guī)則是

6、如下:(假設(shè) path.basename(filepath) = B )· 從B的最后一個(gè).開(kāi)始截取,直到最后一個(gè)字符。· 如果B中不存在.,或者B的第一個(gè)字符就是.,那么返回空字符串。直接看官方文檔的例子1. path.extname('index.html') 2. / returns '.html' 3.  4. path.extname('index.coffee.md') 5. / returns '.md' 6

7、.  7. path.extname('index.') 8. / returns '.' 9.  10. path.extname('index') 11. / returns '' 12.  13. path.extname('.index') 14. / returns ''  路徑組合· path.join(.paths)&

8、#183; path.resolve(.paths)path.join(.paths)把paths拼起來(lái),然后再normalize一下。這句話反正我自己看著也是莫名其妙,可以參考下面的偽代碼定義。例子如下:1. var path = require('path'); 2.  3. / 輸出 '/foo/bar/baz/asdf' 4. path.join('/foo', 'bar', 'baz/asdf', 

9、;'quux', '.');  path定義的偽代碼如下:1. module.exports.join = function() 2.   var paths = Atotye.slice.call(arguments, 0); 3.   return this.normalize( paths.join('/') ); 4. ; &

10、#160;path.resolve(.paths)這個(gè)接口的說(shuō)明有點(diǎn)啰嗦。你可以想象現(xiàn)在你在shell下面,從左到右運(yùn)行一遍cd path命令,最終獲取的絕對(duì)路徑/文件名,就是這個(gè)接口所返回的結(jié)果了。比如 path.resolve('/foo/bar', './baz') 可以看成下面命令的結(jié)果1. cd /foo/bar 2. cd ./baz  更多對(duì)比例子如下:1. var path = require('path'); 2.  3. /&#

11、160;假設(shè)當(dāng)前工作路徑是 /Users/a/Documents/git-code/nodejs-learning-guide/examples/2016.11.08-node-path 4.  5. / 輸出 /Users/a/Documents/git-code/nodejs-learning-guide/examples/2016.11.08-node-path 6. console.log( path.resolve('') ) 7.  8. / 輸出 

12、;/Users/a/Documents/git-code/nodejs-learning-guide/examples/2016.11.08-node-path 9. console.log( path.resolve('.') ) 10.  11. / 輸出 /foo/bar/baz 12. console.log( path.resolve('/foo/bar', './baz') ); 13.  14. /

13、60;輸出 /foo/bar/baz 15. console.log( path.resolve('/foo/bar', './baz/') ); 16.  17. / 輸出 /tmp/file 18. console.log( path.resolve('/foo/bar', '/tmp/file/') ); 19.  20. / 輸出 /Users/a/Doc

14、uments/git-code/nodejs-learning-guide/examples/2016.11.08-node-path/www/js/mod.js 21. console.log( path.resolve('www', 'js/upload', './mod.js') );  路徑解析path.parse(path)path.normalize(filepath)從官方文檔的描述來(lái)看,path.normalize(filepath) 應(yīng)該是比較簡(jiǎn)單的一個(gè)API

15、,不過(guò)用起來(lái)總是覺(jué)得沒(méi)底。為什么呢?API說(shuō)明過(guò)于簡(jiǎn)略了,包括如下:· 如果路徑為空,返回.,相當(dāng)于當(dāng)前的工作路徑。· 將對(duì)路徑中重復(fù)的路徑分隔符(比如linux下的/)合并為一個(gè)。· 對(duì)路徑中的.、.進(jìn)行處理。(類似于shell里的cd .)· 如果路徑最后有/,那么保留該/。感覺(jué)stackoverflow上一個(gè)兄弟對(duì)這個(gè)API的解釋更實(shí)在,原文鏈接。In other words, path.normalize is "What is the shortest path I can take that will take me to the

16、same place as the input"代碼示例如下。建議讀者把代碼拷貝出來(lái)運(yùn)行下,看下實(shí)際效果。1. var path = require('path'); 2. var filepath = '/tmp/demo/js/test.js' 3.  4. var index = 0; 5.  6. var compare = function(desc, callba

17、ck) 7.   console.log('用例%d:%s', +index, desc); 8.   callback(); 9.   console.log('n'); 10. ; 11.  12. compare('路徑為空', function() 13.   / 輸出 . 14.   console.log(&

18、#160;path.normalize('') ); 15. ); 16.  17. compare('路徑結(jié)尾是否帶/', function() 18.   / 輸出 /tmp/demo/js/upload 19.   console.log( path.normalize('/tmp/demo/js/upload') ); 20.  21.   /

19、0;/tmp/demo/js/upload/ 22.   console.log( path.normalize('/tmp/demo/js/upload/') ); 23. ); 24.  25. compare('重復(fù)的/', function() 26.   / 輸出 /tmp/demo/js 27.   console.log( path.normalize('/tmp

20、/demo/js') ); 28. ); 29.  30. compare('路徑帶.', function() 31.   / 輸出 /tmp/demo/js 32.   console.log( path.normalize('/tmp/demo/js/upload/.') ); 33. ); 34.  35. compare('相對(duì)路徑', fu

21、nction() 36.   / 輸出 demo/js/upload/ 37.   console.log( path.normalize('./demo/js/upload/') ); 38.  39.   / 輸出 demo/js/upload/ 40.   console.log( path.normalize('demo/js/upload/') 

22、); 41. ); 42.  43. compare('不常用邊界', function() 44.   / 輸出 . 45.   console.log( path.normalize('./.') ); 46.  47.   / 輸出 . 48.   console.log( path.normalize('.&#

23、39;) ); 49.  50.   / 輸出 ./ 51.   console.log( path.normalize('./') ); 52.  53.   / 輸出 / 54.   console.log( path.normalize('/./') ); 55.    56.  

24、 / 輸出 / 57.   console.log( path.normalize('/.') ); 58. );  感興趣的可以看下 path.normalize(filepath) 的node源碼如下:傳送門(mén)文件路徑分解/組合· path.format(pathObject):將pathObject的root、dir、base、name、ext屬性,按照一定的規(guī)則,組合成一個(gè)文件路徑。· path.parse(filepath):path.for

25、mat()方法的反向操作。我們先來(lái)看看官網(wǎng)對(duì)相關(guān)屬性的說(shuō)明。首先是linux下1.  2.           dir            base     3.              

26、;  4.  root                name  ext  5. "  /    home/user/dir / file  .txt " 6.  7. (all spaces in&

27、#160;the "" line should be ignored - they are purely for formatting)  然后是windows下1.  2.           dir           

28、60;base     3.                4.  root                name  ext  5. " C:  &

29、#160;   pathdir    file  .txt " 6.  7. (all spaces in the "" line should be ignored - they are purely for formatting)  path.format(pathObject)閱讀相

30、關(guān)API文檔說(shuō)明后發(fā)現(xiàn),path.format(pathObject)中,pathObject的配置屬性是可以進(jìn)一步精簡(jiǎn)的。根據(jù)接口的描述來(lái)看,以下兩者是等價(jià)的。· root vs dir:兩者可以互相替換,區(qū)別在于,路徑拼接時(shí),root后不會(huì)自動(dòng)加/,而dir會(huì)。· base vs name+ext:兩者可以互相替換。1. var path = require('path'); 2.  3. var p1 = path.format( 4.   r

31、oot: '/tmp/',  5.   base: 'hello.js' 6. ); 7. console.log( p1 ); / 輸出 /tmp/hello.js 8.  9. var p2 = path.format( 10.   dir: '/tmp',  11.   name:&#

32、160;'hello', 12.   ext: '.js' 13. ); 14. console.log( p2 );  / 輸出 /tmp/hello.js  path.parse(filepath)path.format(pathObject) 的反向操作,直接上官網(wǎng)例子。四個(gè)屬性,對(duì)于使用者是挺便利的,不過(guò)path.format(pathObject) 中也是四個(gè)配置屬性,就有點(diǎn)容易搞混。1. path.parse(&

33、#39;/home/user/dir/file.txt') 2. / returns 3. /  4. /    root : "/", 5. /    dir : "/home/user/dir", 6. /    base : "file.txt", 7. /

34、0;   ext : ".txt", 8. /    name : "file" 9. /   獲取相對(duì)路徑接口:path.relative(from, to)描述:從from路徑,到to路徑的相對(duì)路徑。邊界:· 如果from、to指向同個(gè)路徑,那么,返回空字符串。· 如果from、to中任一者為空,那么,返回當(dāng)前工作路徑。上例子:1. var path =

35、 require('path'); 2.  3. var p1 = path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb'); 4. console.log(p1);  / 輸出 "././impl/bbb" 5.  6. var p2 = path.relative('/dat

36、a/demo', '/data/demo'); 7. console.log(p2);  / 輸出 "" 8.  9. var p3 = path.relative('/data/demo', ''); 10. console.log(p3);  / 輸出 "././Users/a/Documents/git-code/nodejs-learning-guide/examples/2016.11.08-node-path"  平臺(tái)相關(guān)接口/屬性以下屬性、接口,都跟平臺(tái)的具體實(shí)現(xiàn)相關(guān)。也就是說(shuō),同樣的屬性、接口,在不同平臺(tái)上的表現(xiàn)不同。· path.posix:path相關(guān)屬性、接口的linux實(shí)現(xiàn)。· path.win32:path相關(guān)屬性、接口的win32實(shí)現(xiàn)。· path.sep:路徑分隔符。在linux上是/,在windows上是。· path.delimit

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論