path核心模块用于处理文件的绝对路径和目录路径(区别于url模块)
path.join
组合路径名称
path.parse
将文件路径解析成对象
1. path.join
方法
1 2 3 4
| const path = require('path');
let strPath = path.join(_dirname, '///files/', 'jpg', '1.jpg'); console.log(strPath);
|
path.join
方法会自动解析参数中的’/‘符,并组合路径名称

2. path.parse
方法
path/parse
方法用于将文件路径解析成对象。
1 2 3 4 5 6 7
| const path = require('path');
let strPath = path.join(_dirname,'///files/', 'jpg', '1.jpg');
const pathObj = path.parse(strPath);
console.log(pathObj);
|

pathObj
的属性依据原字符串生成,可以改动,但不会对文件路径生效
1 2 3 4 5 6 7 8
| const pathObj = path.parse(strPath);
pathObj.ext = '.txt'; pathObj.name = '2';
console.log(pathObj); console.log(strPath);
|

修改dir属性的唯一入口是base属性,修改base属性能够达到修改文件名信息和文件后缀的效果
1 2 3 4 5 6
| pathObj.base = "newFile.txt"; console.log(pathObj);
let strPathNew = path.formath(pathObj); console.log(strPathNew);
|

