const path = require('path')console.log(__filename)// 获取路径的基础名称=>路径的最后一部分/** * 第一个参数是字符串 * 第二个参数是文件扩展名,有扩展名就不现实扩展名了*/console.log(path.basename(__filename))// test.jsconsole.log(path.basename(__filename,'.js'))// testconsole.log(path.basename('/a/b/c'))// cconsole.log(path.basename('/a/b/c/'))// c//获取目录路径/** * 返回路径中最后一部分的上一层所在目录*/console.log(path.dirname(__filename))// /Users/yangkangweiconsole.log(path.dirname('/a/b/c'))// /a/bconsole.log(path.dirname('/a/b/c/'))// /a/b// 获取文件路径的扩展名console.log(path.extname(__filename))// .jsconsole.log(path.extname('/a/b.html'))// .html// 4 解析路径 const obj1 = path.parse('/a/b/c/index.html')const obj2 = path.parse('./a/b/test.js')// 序列化路径console.log(path.format(obj2))// 判断路径是否为绝对路径console.log(path.isAbsolute('../foo'))console.log(path.isAbsolute('/foo'))//拼接路径console.log(path.join('/a','b','index.html')) console.log(path.join('./a','b','index.html')) console.log(path.join('./a/b','../','index.html')) console.log(path.join('./a/b','./','index.html')) console.log(path.join('')) // 规范化路径console.log(path.normalize('a/b/c/d'))console.log(path.normalize('a///b/c/d'))console.log(path.normalize('a/b\\/c\\/d'))console.log(path.normalize('a/\b/c\\/d'))console.log(path.normalize(''))// 绝对路径console.log(path.resolve('a','/b'))console.log(path.resolve('a','b'))console.log(path.resolve('/a','b'))console.log(path.resolve('index.html')) // 推荐使用