什么是path路径模块
path模块是Node.js 官方提供的、用来处理路径的模块。它提供了一系列的方法和属性,用来满足用户对路径的处理需求。
- path.join0方法,用来将多个路径片段拼接成一个完整的路径字符串
- path.basename0方法,用来从路径字符串中,将文件名解析出来
如果要在JavaScript代码中,使用path模块来处理路径,则需要使用如下的方式先导入它:
path. join([... paths])
参数解读:
- …paths
路径片段的序列 - 返回值:
使用path.join0方法,可以把多个路径片段拼接为完整的路径字符串:
const pathStr = path.join('/a','/b/c','../".*./d','e')
console.log(pathStr)//输 lalbdve
const pathStr2 = path.join(__dirname,'./files/1.txt')
console.log(pathStr2)//输 当前文件所处目录\files11.txt
获取路径中的文件名
path.basename()的语法格式
使用 path.basename0方法,可以获取路径中的最后一部分,经常通过这个方法获取路径中的文件名,语法格式如下:
path.basename(path[, ext])
参数解读:
path
ext
扩展名返回:
const path = require('path')
//定义文件的存放路径
const fpath= '/a/b/cd/index.html'
const fullName = path.basename(fpath)
console.log(fullName)
const nameWithoutExt = path.basename(fpath,'.html')
console.log(nameWithoutExt)
获取路径中的文件扩展名
.path.extname()的语法格式
使用 path.extname()方法,可以获取路径中的扩展名部分,语法格式如下:
const path = require('path')
//文件的存放路径
const fpath ='/a/b/c/d/index.html'
const fext=path.extname(fpath)
console.log(fext)
综合案例-时钟案例
const fs= require('fs')
const path =require('path')
//1.3定义正则表达式,分别匹配<stylex/style>和<script>/script>
const regStyle=/<style>[\s\S]*<\/style>/
const regScript=/<script>[\s\S]*<\/script>/
fs.readFile(path.join(__dirname,'index.html'),'utf8',function (err,dataStr){
if (err){
return console.log("读取错误"+err.message)
}
resolveCSS(dataStr)
resolveJS(dataStr)
resolveHtml(dataStr)
})
function resolveCSS(htmlStr){
const r1=regStyle.exec(htmlStr)
const newCSS= r1[0].replace('<style>','').replace('</style>','')
fs.writeFile(path.join(__dirname,'clock/index.css'),newCSS,function (err){
if (err){
return console.log('写入样式失败'+err.message)
}
console.log('写入成功')
})
}
function resolveJS(htmlStr){
const r2=regScript.exec(htmlStr)
const newScript=r2[0].replace('<script>','').replace('</script>','')
fs.writeFile(path.join(__dirname,'clock/index.js'),newScript,function (err){
if (err){
return console.log('写入Javascript失败'+err.message)
}
console.log('写入JS脚本成功')
})
}
function resolveHtml(htmlStr){
const newHtml= htmlStr.replace(regStyle,'<link rel="stylesheet" href="./index.css"/>').replace(regScript,'<script src="./index.js"><script></script>')
//写入index.html
fs.writeFile(path.join(__dirname,'./clock/index.html'),newHtml,function (err) {
if (err){
return console.log('写入HTML文件失败'+err.message)
}
console.log('写入HTML页面成功!')
})
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>index首页</title>
<style>
html,
baby {
margin: 0;
padding: 0;
height: 100%;
background-image: linear-gradient(to bottom right, red, gold);
}
.box {
width: 400px;
height: 250px;
background-color: rgba(255, 255, 255, 0.6);
border-radius: 6px;
position: absolute;
left: 50%;
top: 40%;
transform: translate(-50%, -50%);
box-shadow: 1px 1px 10px #fff;
text-shadow: 0px 1px 30px white;
display: flex;
justify-content: space-around;
align-items: center;
font-size: 70px;
user-select: none;
padding: 0 20px;
/* 盒子投影*/
-webkit-box-reflect: below 0px -webkit-gradient(linear, left top, left bottom, from(transparent),
color-stop(0%, transparent), to(rgba(250, 250, 250, .2)));
}
</style>
</head>
<body>
<div class="box">
<div id="HH">00</div>
<div>:</div>
<div id="mm">00</div>
<div>:</div>
<div id="ss">00</div>
</div>
<script>
window.onload = function () {
// 定时器,每隔1秒执行1次
setInterval(() => {
var dt = new Date()
var HH = dt.getHours()
var mm = dt.getMinutes()
var ss = dt.getSeconds()
// 为页面上的元素赋值
document.querySelector('#HH').innerHTML = padZero(HH)
document.querySelector('#mm').innerHTML = padZero(mm)
document.querySelector('#ss').innerHTML = padZero(ss)
}, 1000)
}
// 补零函数
function padZero(n) {
return n > 9 ? n : '0' + n
}
</script>
</body>
</html>