在没有相关经验的同窗,在学习webpack和使用node时,常常会遇到__dirname
path.resolve
等,这时每每会一脸懵逼这些都是什么,干吗的,为何这些资料和书都彻底不提,难道就我不知道 -。-html
其实path模块是很常见很通用的。这里我就简单总结一些在项目中常常会用到的方法,帮助你们尽快掌握。node
path.normalize('C:\\temp\\\\foo\\bar\\..\\');
// 返回: 'C:\\temp\\foo\\'
var myPath = path.normalize(__dirname + '/test/a//b//../c/helloWorld.js');
console.log(myPath); //windows: E:\workspace\NodeJS\app\fs\test\a\c\helloWorld.js
复制代码
PS:webpack
例子以下:git
var path = require('path');
var filepath = '/tmp/demo/js/test.js';
// 输出:/tmp/demo/js
console.log( path.dirname(filepath) );
var myPath = path.dirname(__dirname + '/test/util/helloWorld.js');
console.log(myPath);
//Users/cayley/Documents/webpack-demo/test/util
复制代码
__dirname是node.js中的一个全局变量,用来获取当前模块文件所在目录的完整绝对路径web
严格意义上来讲,path.basename(filepath) 只是输出路径的最后一部分,并不会判断是否文件名。windows
但大部分时候,咱们能够用它来做为简易的“获取文件名“的方法。api
var path = require('path');
// 输出:test.js
console.log( path.basename('/tmp/demo/js/test.js') );
// 输出:test
console.log( path.basename('/tmp/demo/js/test/') );
// 输出:test
console.log( path.basename('/tmp/demo/js/test') );
复制代码
若是只想获取文件名,单不包括文件扩展呢?能够用上第二个参数。bash
// 输出:test
console.log( path.basename('/tmp/demo/js/test.js', '.js') );
复制代码
简单的例子以下:网络
var path = require('path');
var filepath = '/tmp/demo/js/test.js';
// 输出:.js
console.log( path.extname(filepath) );
复制代码
更详细的规则是以下:(假设 path.basename(filepath) === B )app
从B的最后一个.
开始截取,直到最后一个字符。
若是B中不存在.
,或者B的第一个字符就是.
,那么返回空字符串。
直接看官方文档的例子
path.extname('index.html')
// returns '.html'
path.extname('index.coffee.md')
// returns '.md'
path.extname('index.')
// returns '.'
path.extname('index')
// returns ''
path.extname('.index')
// returns ''
复制代码
path.join([...paths])
path.resolve([...paths])
path.join()
方法使用平台特定的分隔符把所有给定的 path 片断链接到一块儿,并规范化生成的路径。 长度为零的 path 片断会被忽略。 若是链接后的路径字符串是一个长度为零的字符串,则返回 '.',表示当前工做目录。
参数说明: ...paths <string>
一个路径片断的序列。 返回:
把paths
拼起来,而后再normalize一下。意思就是会先把路径拼接在一块儿,而后进行规范化,返回正确的路径。
例子以下:
var path = require('path');
// 输出 '/foo/bar/baz/asdf'
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
复制代码
path定义的伪代码以下:
module.exports.join = function(){
var paths = Array.prototye.slice.call(arguments, 0);
return this.normalize( paths.join('/') );
};
复制代码
更多例子:
path.join('/foo', 'bar', 'baz/asdf', 'quux', '.'); // 返回 /foo/bar/baz/asdf/quux, "."和"/"没什么影响
path.join('/foo', './bar', 'baz/asdf', '.', 'quux'); // 返回 /foo/bar/baz/asdf/quux
path.join('/foo', './bar', './baz/asdf', 'quux', '..'); // 返回 /foo/bar/baz/asdf
path.join('/foo', 'bar', 'baz/asdf', '.', '.'); // 返回 /foo/bar/baz/asdf
path.join('/foo', 'bar', 'baz/asdf', 'quux'); // 返回 /foo/bar/baz/asdf/quux
path.join('/foo', 'bar', 'baz/asdf', '..', '..'); // 返回 /foo/bar
复制代码
智能解析绝对路径 path.resolve() 方法会把一个路径或路径片断的序列解析为一个绝对路径。层级关系是从左到右的.
规则:
1\. 给定的路径的序列是从右往左被处理的,后面每一个 path 被依次解析,直到构造完成一个绝对路径。
2\. 若是处理彻底部给定的 path 片断后还未生成一个绝对路径,则当前工做目录会被用上。
3\. 生成的路径是规范化后的,且末尾的斜杠会被删除,除非路径被解析为根目录。
4\. 长度为零的 path 片断会被忽略。
5\. 若是没有传入 path 片断,则 path.resolve() 会返回当前工做目录的绝对路径。
6\. path.resolve将以/开始的路径片断做为根目录,在此以前的路径将会被丢弃,就像是在terminal中使用cd命令同样。而path.join只是简单的将该路径片断进行拼接
复制代码
好比 path.resolve('/foo/bar', './baz')
能够当作下面命令的结果
cd /foo/bar
cd ./baz
复制代码
更多对比例子以下:
var path = require('path');
// 假设当前工做路径是 /Users/a/Documents/git-code/nodejs-learning-guide/examples/2016.11.08-node-path
// 输出 /Users/a/Documents/git-code/nodejs-learning-guide/examples/2016.11.08-node-path
console.log( path.resolve('') )
// 输出 /Users/a/Documents/git-code/nodejs-learning-guide/examples/2016.11.08-node-path
console.log( path.resolve('.') )
// 输出 /foo/bar/baz
console.log( path.resolve('/foo/bar', './baz') );
// 输出 /foo/bar/baz
console.log( path.resolve('/foo/bar', './baz/') );
// 输出 /tmp/file
console.log( path.resolve('/foo/bar', '/tmp/file/') );
// 输出 /Users/a/Documents/git-code/nodejs-learning-guide/examples/2016.11.08-node-path/www/js/mod.js
console.log( path.resolve('www', 'js/upload', '../mod.js') );
复制代码
更多例子:
// 当前工做目录与当前文件路径(F:/1/2/task6/test/dist)有区别
path.resolve(); // F:/1/2/task6/test 当前工做目录的绝对路径
path.resolve('./a'); // F:/1/2/task6/test/a
path.resolve('../a'); // F:/1/2/task6/a
path.resolve('.'); // F:/1/2/task6/test
path.resolve('..'); // F:/1/2/task6
path.resolve('/')); // F:/
path.resolve('./a','../c/d'); // F:/1/2/task6/test/c/d
path.resolve('./a','./c/d'); // F:/1/2/task6/test/a/c/d
path.resolve('/a','../c/d'); // F:c/d
path.resolve('/a','./c/d'); // F:/a/c/d
path.resolve('./a','/b','./c/d'); // F:/b/c/d
path.resolve('a','b','c/d'); // F:/1/2/task6/test/a/b/c/d
path.resolve('./a','./b','c/d'); // F:/1/2/task6/test/a/b/c/d
path.resolve('./a','/b','c/d'); // F:/b/c/d
path.resolve('./a/b','..','c/d'); // F:/1/2/task6/test/a/c/d
path.resolve('./a','..','c/d'); // F:/1/2/task6/test/c/d
复制代码
接口:path.relative(from, to)
描述:从from
路径,到to
路径的相对路径。
边界:
若是from
、to
指向同个路径,那么,返回空字符串。
若是from
、to
中任一者为空,那么,返回当前工做路径。
上例子:
var path = require('path');
var p1 = path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');
console.log(p1); // 输出 "../../impl/bbb"
var p2 = path.relative('/data/demo', '/data/demo');
console.log(p2); // 输出 ""
var p3 = path.relative('/data/demo', '');
console.log(p3); // 输出 "../../Users/a/Documents/git-code/nodejs-learning-guide/examples/2016.11.08-node-path"
复制代码