NodeJS入門(四)—— path對象

很快Node就會迎來4.0的時代,屆時將併入現有的iojs,因此先前寫過的iojs入門系列直接改名爲NodeJS入門。html

本篇開始將逐個介紹Node的各主要模塊,依循API文檔走一遍,但會給出比API文檔更詳盡的示例。java

全部的示例均可以在個人Github上下載到。node

API系列的開篇打算以 path 對象開始,由於以前在寫一些gulp插件,發現path的使用頻率實在過高了,掌握之有助於咱們處理好一些文件或文件夾的路徑。git

path.normalize(p)github

標準化路徑字符串,處理冗餘的「..」、「.」、「/」字符:gulp

var path = require('path');

var url1 = path.normalize('a/b/c/../user/vajoy/bin');
var url2 = path.normalize('a/b/c///../user/vajoy/bin/');
var url3 = path.normalize('a/b/c/../../user/vajoy/bin');
var url4 = path.normalize('a/b/c/.././///../user/vajoy/bin/..');
var url5 = path.normalize('a/b/c/../../user/vajoy/bin/../../');
var url6 = path.normalize('a/../../user/vajoy/bin/../../');
var url7 = path.normalize('a/../../user/vajoy/bin/../../../../');
var url8 = path.normalize('./a/.././user/vajoy/bin/./');

console.log('url1:',url1);  // a\b\user\vajoy\bin
console.log('url2:',url2);  // a\b\user\vajoy\bin\
console.log('url3:',url3);  // a\user\vajoy\bin
console.log('url4:',url4);  // a\user\vajoy
console.log('url5:',url5);  // a\user\
console.log('url6:',url6);  // ..\user\
console.log('url7:',url7);  // ..\..\
console.log('url8:',url8);  // user\vajoy\bin\

path.join([path1], [path2], [...])ui

將多個路徑結合在一塊兒,並轉換爲標準化的路徑:url

var path = require('path');

var url1 = path.join('////./a', 'b////c', 'user/', 'vajoy', '..');
var url2 = path.join('a', '../../', 'user/', 'vajoy', '..');
var url3 = path.join('a', '../../', {}, 'vajoy', '..');

console.log('url1:',url1);  // \a\b\c\user
console.log('url2:',url2);  // ..\user
console.log('url3:',url3);  // 存在非路徑字符串,故拋出異常

path.resolve([from ...], to)spa

從源地址 from 到目的地址 to 的絕對路徑。插件

能夠理解爲 cd XXX 的形式,如在D盤上執行 path.resolve('a', 'D:/b', '../c', 'v.txt'),獲得的絕對路徑「D:/v.txt」,至關於執行以下指令後所處的路徑:

cd a
D:
cd b  //同上一行對應 'D:/b'
cd ../c
cd v.txt

要注意的是,若是某個 from 或 to 參數是絕對路徑(好比 'E:/abc',或是以「/」開頭的路徑),則將忽略以前的 from 參數。

示例:

var path = require('path');

var url1 = path.resolve('.', 'testFiles/..', 'trdLayer');
var url2 = path.resolve('..', 'testFiles', 'a.txt');
var url3 = path.resolve('D:/vajoy', 'abc', 'D:/a');
var url4 = path.resolve('abc', 'vajoy', 'ok.gif');
var url5 = path.resolve('abc', '/vajoy', '..', 'a/../subfile'); //'abc'參數將被忽略,源路徑改從'E:/vajoy'開始

console.log('url1:',url1);  // E:\github\nodeAPI\path\trdLayer
console.log('url2:',url2);  // E:\github\nodeAPI\testFiles\a.txt
console.log('url3:',url3);  // D:\a
console.log('url4:',url4);  // E:\github\nodeAPI\path\abc\vajoy\ok.gif
console.log('url5:',url5);  // E:\subfile

path.isAbsolute(path)

判斷 path 是否絕對路徑。這塊能夠理解爲,path 是否真的是一個絕對路徑(好比 'E:/abc'),或者是以「/」開頭的路徑,兩者都會返回true:

var path = require('path');

var url1 = path.isAbsolute('../testFiles/secLayer');
var url2 = path.isAbsolute('./join.js');
var url3 = path.isAbsolute('temp');
var url4 = path.isAbsolute('/temp/../..');
var url5 = path.isAbsolute('E:/github/nodeAPI/abc/efg');
var url6 = path.isAbsolute('///temp123');

console.log('url1:',url1);  // false
console.log('url2:',url2);  // false
console.log('url3:',url3);  // false
console.log('url4:',url4);  // true
console.log('url5:',url5);  // true
console.log('url6:',url6);  // true

path.relative(from, to)

獲取從 from 到 to 的相對路徑,能夠看做 path.resolve 的相反實現:

path.resolve(from, path.relative(from, to)) == path.resolve(to)

示例:

var path = require('path');

var url1 = path.relative('C:\\vajoy\\test\\aaa', 'C:\\vajoy\\impl\\bbb');
var url2 = path.relative('C:/vajoy/test/aaa', 'C:/vajoy/bbb');
var url3 = path.relative('C:/vajoy/test/aaa', 'D:/vajoy/bbb');

console.log('url1:',url1);  //..\..\impl\bbb
console.log('url2:',url2);  //url2: ..\..\bbb
console.log('url3:',url3);  //D:\vajoy\bbb

path.dirname(p)

返回路徑中文件夾的路徑:

var path = require('path');

var url1 = path.dirname('/foo/bar/baz/asdf/a.txt');
var url2 = path.dirname('/foo/bar/baz/asdf/');
var url3 = path.dirname('C:/vajoy/test/aaa');

console.log('url1:',url1);  // /foo/bar/baz/asdf
console.log('url2:',url2);  // /foo/bar/baz
console.log('url3:',url3);  // C:/vajoy/test

path.basename(p, [ext])

返回路徑中的最後一部分,相似於Unix 的 basename 命令。 ext 爲須要截掉的尾綴內容:

var path = require('path');

var url1 = path.basename('/foo/bar/baz/asdf/a.txt');
var url2 = path.basename('/foo/bar/baz/asdf/a.txt','.txt');
var url3 = path.basename('/foo/bar/baz/asdf/');
var url4 = path.basename('C:/vajoy/test/aaa');

console.log('url1:',url1);  // a.txt
console.log('url2:',url2);  // a
console.log('url3:',url3);  // asdf
console.log('url4:',url4);  // aaa

path.extname(p)

返回路徑文件中的擴展名(若存在):

var path = require('path');

var url1 = path.extname('/foo/bar/baz/asdf/a.txt');
var url2 = path.extname('/foo/bar/baz/asdf/a.txt.html');
var url3 = path.extname('/foo/bar/baz/asdf/a.');
var url4 = path.extname('C:/vajoy/test/.');
var url5 = path.extname('C:/vajoy/test/a');

console.log('url1:',url1);  // .txt
console.log('url2:',url2);  // .html
console.log('url3:',url3);  // .
console.log('url4:',url4);  //
console.log('url5:',url5);  //

path.sep

返回對應平臺下的文件夾分隔符,win下爲'\',*nix下爲'/':

var path = require('path');

var url1 = path.sep;
var url2 = 'foo\\bar\\baz'.split(path.sep);
var url3 = 'foo/bar/baz'.split(path.sep);

console.log('url1:',url1);  // win下爲\,*nix下爲/
console.log('url2:',url2);  // [ 'foo', 'bar', 'baz' ]
console.log('url3:',url3);  // win下返回[ 'foo/bar/baz' ],但在*nix系統下會返回[ 'foo', 'bar', 'baz' ]

path.delimiter

返回對應平臺下的路徑分隔符,win下爲';',*nix下爲':':

var path = require('path');
var env = process.env.PATH; //當前系統的環境變量PATH

var url1 = env.split(path.delimiter);

console.log(path.delimiter); //win下爲「;」,*nix下爲「:」
console.log('env:',env);  // C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;
console.log('url1:',url1);  // ['C:\ProgramData\Oracle\Java\javapath','C:\Program Files (x86)\Intel\iCLS Client\']

 

path的API較少,咱們就介紹到這裏,共勉~

donate

相關文章
相關標籤/搜索