1. const path = require('path')
    2. console.log(__filename)
    3. // 获取路径的基础名称=>路径的最后一部分
    4. /**
    5. * 第一个参数是字符串
    6. * 第二个参数是文件扩展名,有扩展名就不现实扩展名了
    7. */
    8. console.log(path.basename(__filename))
    9. // test.js
    10. console.log(path.basename(__filename,'.js'))
    11. // test
    12. console.log(path.basename('/a/b/c'))
    13. // c
    14. console.log(path.basename('/a/b/c/'))
    15. // c
    16. //获取目录路径
    17. /**
    18. * 返回路径中最后一部分的上一层所在目录
    19. */
    20. console.log(path.dirname(__filename))
    21. // /Users/yangkangwei
    22. console.log(path.dirname('/a/b/c'))
    23. // /a/b
    24. console.log(path.dirname('/a/b/c/'))
    25. // /a/b
    26. // 获取文件路径的扩展名
    27. console.log(path.extname(__filename))
    28. // .js
    29. console.log(path.extname('/a/b.html'))
    30. // .html
    31. // 4 解析路径
    32. const obj1 = path.parse('/a/b/c/index.html')
    33. const obj2 = path.parse('./a/b/test.js')
    34. // 序列化路径
    35. console.log(path.format(obj2))
    36. // 判断路径是否为绝对路径
    37. console.log(path.isAbsolute('../foo'))
    38. console.log(path.isAbsolute('/foo'))
    39. //拼接路径
    40. console.log(path.join('/a','b','index.html'))
    41. console.log(path.join('./a','b','index.html'))
    42. console.log(path.join('./a/b','../','index.html'))
    43. console.log(path.join('./a/b','./','index.html'))
    44. console.log(path.join(''))
    45. // 规范化路径
    46. console.log(path.normalize('a/b/c/d'))
    47. console.log(path.normalize('a///b/c/d'))
    48. console.log(path.normalize('a/b\\/c\\/d'))
    49. console.log(path.normalize('a/\b/c\\/d'))
    50. console.log(path.normalize(''))
    51. // 绝对路径
    52. console.log(path.resolve('a','/b'))
    53. console.log(path.resolve('a','b'))
    54. console.log(path.resolve('/a','b'))
    55. console.log(path.resolve('index.html')) // 推荐使用