Nodejs path对象

很快Node就会迎来4.0的时代,届时将并入现有的iojs,所以先前写过的iojs入门系列直接更名为NodeJS入门。

本篇开始将逐个介绍Node的各主要模块,依循API文档走一遍,但会给出比API文档更详尽的示例。

所有的示例都可以在Github上下载到。

API系列的开篇打算以 path 对象开始,因为之前在写一些gulp插件,发现path的使用频率实在太高了,掌握之有助于我们处理好一些文件或文件夹的路径。

path.normalize(p)

标准化路径字符串,处理冗余的“..”、“.”、“/”字符:

[复制代码](javascript:void(0)😉

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\

[复制代码](javascript:void(0)😉

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

将多个路径结合在一起,并转换为标准化的路径:

[复制代码](javascript:void(0)😉

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);  // 存在非路径字符串,故抛出异常

[复制代码](javascript:void(0)😉

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

从源地址 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 参数。

示例:

[复制代码](javascript:void(0)😉

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

[复制代码](javascript:void(0)😉

path.isAbsolute(path)**
**

判断 path 是否绝对路径。这块可以理解为,path 是否真的是一个绝对路径(比如 'E:/abc'),或者是以“/”开头的路径,二者都会返回true:

[复制代码](javascript:void(0)😉

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

[复制代码](javascript:void(0)😉

path.relative(from, to)

获取从 from 到 to 的相对路径,可以看作 path.resolve 的相反实现:

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

示例:

[复制代码](javascript:void(0)😉

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

[复制代码](javascript:void(0)😉

path.dirname(p)

返回路径中文件夹的路径:

[复制代码](javascript:void(0)😉

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

[复制代码](javascript:void(0)😉

path.basename(p, [ext])

返回路径中的最后一部分,类似于Unix 的 basename 命令。 ext 为需要截掉的尾缀内容:

[复制代码](javascript:void(0)😉

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

[复制代码](javascript:void(0)😉

path.extname(p)

返回路径文件中的扩展名(若存在):

[复制代码](javascript:void(0)😉

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);  //

[复制代码](javascript:void(0)😉

path.sep

返回对应平台下的文件夹分隔符,win下为'',*nix下为'/':

[复制代码](javascript:void(0)😉

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' ]

[复制代码](javascript:void(0)😉

path.delimiter

返回对应平台下的路径分隔符,win下为';',*nix下为':':

[复制代码](javascript:void(0)😉

var path = require('path');
var env = process.env.PATH; //当前系统的环境变量PATHvar 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\']

[复制代码](javascript:void(0)😉

path的API较少,我们就介绍到这里,共勉~

posted @ 2018-11-02 16:32  诗和远方-ysk  阅读(143)  评论(0编辑  收藏  举报