Node.js Path 模块

Node.js path 模块提供了一些用于处理文件路径的小工具,我们可以通过以下方式引入该模块:

var path = require("path");
  • 对window系统,目录分隔为'',对于UNIX系统,分隔符为'/',针对'..'返回上一级;/与\都被统一转换

1、path.normalize(p)

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

var myPath = path.normalize(__dirname + '/test/a//b//../c/utilyou.mp3');
console.log(myPath); //windows: E:\workspace\NodeJS\app\fs\test\a\c\utilyou.mp3

var normalize1 = path.normalize('a/b/c/../user/vajoy/bin');
var normalize2 = path.normalize('a/b/c///../user/vajoy/bin/');
var normalize3 = path.normalize('a/b/c/../../user/vajoy/bin');
var normalize4 = path.normalize('a/b/c/.././///../user/vajoy/bin/..');
var normalize5 = path.normalize('a/b/c/../../user/vajoy/bin/../../');
var normalize6 = path.normalize('a/../../user/vajoy/bin/../../');
var normalize7 = path.normalize('a/../../user/vajoy/bin/../../../../');
var normalize8 = path.normalize('./a/.././user/vajoy/bin/./');
console.log(normalize1);  // a\b\user\vajoy\bin
console.log(normalize2);  // a\b\user\vajoy\bin\
console.log(normalize3);  // a\user\vajoy\bin
console.log(normalize4);  // a\user\vajoy
console.log(normalize5);  // a\user\
console.log(normalize6);  // ..\user\
console.log(normalize7);  // ..\..\
console.log(normalize8);  // user\vajoy\bin\

2、path.join([path1],[path2]..[pathn])

//将多个路径结合在一起,并转换为标准化的路径
/** 
*  [path1] 路径或表示目录的字符
**/

var path1 = 'path1', 
    path2 = 'path2//pp\\', 
    path3 = '../path3';
var myPath = path.join(path1, path2, path3);
console.log(myPath); /* path1\path2\path3  */

var join1 = path.join('////./a', 'b////c', 'user/', 'vajoy', '..');
var join2 = path.join('a', '../../', 'user/', 'vajoy', '..');
var join3 = path.join('a', '../../', {}, 'vajoy', '..');
console.log(join1);  // \a\b\c\user
console.log(join2);  // ..\user
console.log(join3);  // 存在非路径字符串,故抛出异常

3、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 参数。
*/

var resolve1 = path.resolve('.', 'testFiles/..', 'trdLayer');
var resolve2 = path.resolve('..', 'testFiles', 'a.txt');
var resolve3 = path.resolve('D:/vajoy', 'abc', 'D:/a');
var resolve4 = path.resolve('abc', 'vajoy', 'ok.gif');
//'abc'参数将被忽略,源路径改从'E:/vajoy'开始
var resolve5 = path.resolve('abc', '/vajoy', '..', 'a/../subfile');  

console.log(resolve1);  // E:\github\nodeAPI\path\trdLayer
console.log(resolve2);  // E:\github\nodeAPI\testFiles\a.txt
console.log(resolve3);  // D:\a
console.log(resolve4);  // E:\github\nodeAPI\path\abc\vajoy\ok.gif
console.log(resolve5);  // E:\subfile

4、path.isAbsolute(path)

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

var isAbsolute1 = path.isAbsolute('../testFiles/secLayer');
var isAbsolute2 = path.isAbsolute('./join.js');
var isAbsolute3 = path.isAbsolute('temp');
var isAbsolute4 = path.isAbsolute('/temp/../..');
var isAbsolute5 = path.isAbsolute('E:/github/nodeAPI/abc/efg');
var isAbsolute6 = path.isAbsolute('///temp123');

console.log(isAbsolute1);  // false
console.log(isAbsolute2);  // false
console.log(isAbsolute3);  // false
console.log(isAbsolute4);  // true
console.log(isAbsolute5);  // true
console.log(isAbsolute6);  // true

5、path.relative(from, to)

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

//from 当前路径,并且方法返回值是基于from指定到to的相对路径 * to 到哪路径
var from = 'c:\\from\\a\\', 
    to = 'c:/test/b';
var _path = path.relative(from, to);
console.log(_path);  /*  ..\..\test\b; 表示从from到to的相对路径  */

//示例:
var relative1 = path.relative('C:\\vajoy\\test\\aaa', 'C:\\vajoy\\impl\\bbb');
var relative2 = path.relative('C:/vajoy/test/aaa', 'C:/vajoy/bbb');
var relative3 = path.relative('C:/vajoy/test/aaa', 'D:/vajoy/bbb');

console.log(relative1);  //..\..\impl\bbb
console.log(relative2);  //..\..\bbb
console.log(relative3);  //D:\vajoy\bbb

6、path.dirname(p)

//获取路径中目录名

var dirname1 = path.dirname('/foo/bar/baz/asdf/a.txt');
var dirname2 = path.dirname('/foo/bar/baz/asdf/');
var dirname3 = path.dirname('C:/vajoy/test/aaa');

console.log(dirname1);   // /foo/bar/baz/asdf
console.log(dirname2);   // /foo/bar/baz
console.log(dirname3);   // C:/vajoy/test

7、path.basename(p, [ext])

/*
返回路径中的最后一部分,类似于Unix 的 basename 命令。ext 为需要截掉的尾缀内容
后缀是可选的,如果加,请使用'.ext'方式来匹配,则返回值中不包括后缀名
*/

var basename1 = path.basename('/foo/bar/baz/asdf/a.txt');
var basename2 = path.basename('/foo/bar/baz/asdf/a.txt','.txt');
var basename3 = path.basename('/foo/bar/baz/asdf/you.mp3');
var basename4 = path.basename('/foo/bar/baz/asdf/you.mp3','.mp3');
var basename5 = path.basename('/foo/bar/baz/asdf/');
var basename6 = path.basename('C:/vajoy/test/aaa');

console.log(basename1);  // a.txt
console.log(basename2);  // a
console.log(basename3);  // you.mp3
console.log(basename4);  // you
console.log(basename5);  // asdf
console.log(basename6);  // aaa

8、path.extname(p)

//返回路径文件中的扩展名(若存在),如果没有'.',则返回空

var extname1 = path.extname('/foo/bar/baz/asdf/a.txt');
var extname2 = path.extname('/foo/bar/baz/asdf/a.txt.html');
var extname3 = path.extname('/foo/bar/baz/asdf/a.');
var extname4 = path.extname('C:/vajoy/test/.');
var extname5 = path.extname('C:/vajoy/test/a');

console.log(extname1);  // .txt
console.log(extname2);  // .html
console.log(extname3);  // .
console.log(extname4);  //
console.log(extname5);  //

9、path.sep

//返回操作系统中文件分隔符; window是'\\', Unix是'/'

var sep1 = path.sep;
var sep2 = 'foo\\bar\\baz'.split(path.sep);
var sep3 = 'foo/bar/baz'.split(path.sep);

console.log(sep1);  // window下为\,Unix下为/
console.log(sep2);  // ['foo', 'bar', 'baz']
console.log(sep3);  // window下返回[ 'foo/bar/baz' ],但在Unix系统下会返回[ 'foo', 'bar', 'baz' ]

10、path.delimiter属性

//返回操作系统中目录分隔符,如window是';', Unix中是':'

你可能感兴趣的:(Node.js Path 模块)