获取node的内置模块path
const Path = require("path") // 导入内置模块
console.log(Path); // 打印path模块内容
路径拼接方法 join
(在不同的操作系统中会自动转换 / \ 两者)
console.log(Path.join("D:/WEB","/node/02"));
console.log("D:/WEB"+"/node/02"); // 字符串拼接不会转为 \
console.log(__dirname); // 文件的父级目录的绝对路径
console.log(__filename); // 文件的绝对路径
// 将父级目录与当前文件拼接得到当前文件的绝对路径
console.log( Path.join(__dirname,"05-path模块.js"));
// join方法不传参数默认是 ./
console.log( Path.join()); // .
转绝对路径 resolve
执行resolve方法会先获取父级目录的绝对路径
console.log( Path.resolve("../", "05-path模块.js")); // D:\WEB\node\05-path模块.js
console.log(Path.resolve()); // D:\WEB\node\02
转相对路径 relative
console.log(Path.relative(__dirname, "05-path模块.js"));
将路径解析为对象 parse
let fileSrc = Path.join(__dirname,"05-path模块.js")
console.log(Path.parse(fileSrc));
获取路径中的文件名
console.log(Path.basename(fileSrc));
获取路径中文件的扩展名(后缀名)
console.log(Path.extname(fileSrc));
format parse的反向操作,将路径对象解析为路径
console.log(Path.format(Path.parse(fileSrc)));
规范路径 normalize
console.log(Path.normalize("D:\\WEB\\node\\02"));