什么是path路径模块

path模块是Node.js 官方提供的、用来处理路径的模块。它提供了一系列的方法和属性,用来满足用户对路径的处理需求。

  • path.join0方法,用来将多个路径片段拼接成一个完整的路径字符串
  • path.basename0方法,用来从路径字符串中,将文件名解析出来

如果要在JavaScript代码中,使用path模块来处理路径,则需要使用如下的方式先导入它:

  1. path. join([... paths])

参数解读:

  • …paths 路径片段的序列
  • 返回值:

使用path.join0方法,可以把多个路径片段拼接为完整的路径字符串:

  1. const pathStr = path.join('/a''/b/c''../".*./d''e')
  2. console.log(pathStr)//输 lalbdve
  3. const pathStr2 = path.join(__dirname'./files/1.txt')
  4. console.log(pathStr2)//输 当前文件所处目录\files11.txt

获取路径中的文件名

path.basename()的语法格式
使用 path.basename0方法,可以获取路径中的最后一部分,经常通过这个方法获取路径中的文件名,语法格式如下:

  1. path.basename(path[, ext])

参数解读:
path 必选参数,表示一个路径的字符串
ext 可选参数,表示文件
扩展名返回:表示路径中的最后一部分

  1. const path = require('path')
  2. //定义文件的存放路径
  3. const fpath= '/a/b/cd/index.html'
  4. const fullName = path.basename(fpath)
  5. console.log(fullName)
  6. const nameWithoutExt = path.basename(fpath,'.html')
  7. console.log(nameWithoutExt)

获取路径中的文件扩展名

.path.extname()的语法格式
使用 path.extname()方法,可以获取路径中的扩展名部分,语法格式如下:

  1. const path = require('path')
  2. //文件的存放路径
  3. const fpath ='/a/b/c/d/index.html'
  4. const fext=path.extname(fpath)
  5. console.log(fext)

综合案例-时钟案例

  1. const fs= require('fs')
  2. const path =require('path')
  3. //1.3定义正则表达式,分别匹配<stylex/style>和<script>/script>
  4. const regStyle=/<style>[\s\S]*<\/style>/
  5. const regScript=/<script>[\s\S]*<\/script>/
  6. fs.readFile(path.join(__dirname,'index.html'),'utf8',function (err,dataStr){
  7. if (err){
  8. return console.log("读取错误"+err.message)
  9. }
  10. resolveCSS(dataStr)
  11. resolveJS(dataStr)
  12. resolveHtml(dataStr)
  13. })
  14. function resolveCSS(htmlStr){
  15. const r1=regStyle.exec(htmlStr)
  16. const newCSS= r1[0].replace('<style>','').replace('</style>','')
  17. fs.writeFile(path.join(__dirname,'clock/index.css'),newCSS,function (err){
  18. if (err){
  19. return console.log('写入样式失败'+err.message)
  20. }
  21. console.log('写入成功')
  22. })
  23. }
  24. function resolveJS(htmlStr){
  25. const r2=regScript.exec(htmlStr)
  26. const newScript=r2[0].replace('<script>','').replace('</script>','')
  27. fs.writeFile(path.join(__dirname,'clock/index.js'),newScript,function (err){
  28. if (err){
  29. return console.log('写入Javascript失败'+err.message)
  30. }
  31. console.log('写入JS脚本成功')
  32. })
  33. }
  34. function resolveHtml(htmlStr){
  35. const newHtml= htmlStr.replace(regStyle,'<link rel="stylesheet" href="./index.css"/>').replace(regScript,'<script src="./index.js"><script></script>')
  36. //写入index.html
  37. fs.writeFile(path.join(__dirname,'./clock/index.html'),newHtml,function (err) {
  38. if (err){
  39. return console.log('写入HTML文件失败'+err.message)
  40. }
  41. console.log('写入HTML页面成功!')
  42. })
  43. }
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>index首页</title>
  8. <style>
  9. html,
  10. baby {
  11. margin: 0;
  12. padding: 0;
  13. height: 100%;
  14. background-image: linear-gradient(to bottom right, red, gold);
  15. }
  16. .box {
  17. width: 400px;
  18. height: 250px;
  19. background-color: rgba(255, 255, 255, 0.6);
  20. border-radius: 6px;
  21. position: absolute;
  22. left: 50%;
  23. top: 40%;
  24. transform: translate(-50%, -50%);
  25. box-shadow: 1px 1px 10px #fff;
  26. text-shadow: 0px 1px 30px white;
  27. display: flex;
  28. justify-content: space-around;
  29. align-items: center;
  30. font-size: 70px;
  31. user-select: none;
  32. padding: 0 20px;
  33. /* 盒子投影*/
  34. -webkit-box-reflect: below 0px -webkit-gradient(linear, left top, left bottom, from(transparent),
  35. color-stop(0%, transparent), to(rgba(250, 250, 250, .2)));
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <div class="box">
  41. <div id="HH">00</div>
  42. <div>:</div>
  43. <div id="mm">00</div>
  44. <div>:</div>
  45. <div id="ss">00</div>
  46. </div>
  47. <script>
  48. window.onload = function () {
  49. // 定时器,每隔1秒执行1次
  50. setInterval(() => {
  51. var dt = new Date()
  52. var HH = dt.getHours()
  53. var mm = dt.getMinutes()
  54. var ss = dt.getSeconds()
  55. // 为页面上的元素赋值
  56. document.querySelector('#HH').innerHTML = padZero(HH)
  57. document.querySelector('#mm').innerHTML = padZero(mm)
  58. document.querySelector('#ss').innerHTML = padZero(ss)
  59. }, 1000)
  60. }
  61. // 补零函数
  62. function padZero(n) {
  63. return n > 9 ? n : '0' + n
  64. }
  65. </script>
  66. </body>
  67. </html>