黑马node.js Day2(时钟案例代码)

<!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,
        body {
            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>

这是html素材

拆分代码以及解释如下

// 1.1导入fs模块
const fs = require('fs')
// 1.2导入path  路径处理模块
const path = require('path')

// 1.3定义正则表达式,分别匹配 <style></style>和<script></script>标签
// 注意要用转义符  转义</style >的杠
// \s匹配空白字符 \S匹配任意非空白字符 *表示匹配任意次
const regStyle = /<style>[\s\S]*<\/style>/ //代表这个标签内的所有内容都要
const regScript = /<script>[\s\S]*<\/script>/
// 2.1读取需要处理的HTML文件
fs.readFile(path.join(__dirname, 'h电子时钟案例.html'), 'utf8', (err, dataStr) => {
    if (err) {
        // 2.2读取html文件失败
        return console.log('读取html文件失败!' + err.message);
    }
    // 2.3读取HTML文件成功,调用对应的方法,拆解出html,css和html文件
    resolveCSS(dataStr)
    resolveJS(dataStr)
    resolveHTML(dataStr)
})

// 3.1定义处理css样式方法
function resolveCSS(htmlStr) {
    // 3.2使用正则提取需要的内容 
    //  JavaScript的exec方法是检索字符串中正则表达式的匹配
    // 未找到匹配等于null 找到会返回一个数组 其中存放匹配的结果
    // 这个数组的第0个元素是与正则表达式相匹配的文本
    const r1 = regStyle.exec(htmlStr)
    // console.log(r1);   
    // 3.3将提取出来的样式字符串,进行字符串的replace替换操作
    const newCSS = r1[0].replace('<style>', '').replace('</style>', '')
    // console.log(newCSS);
    // 3.4调用fs.writeFile()方法,将提取的样式,写入 index.css文件里面
    fs.writeFile(path.join(__dirname, 'index.css'), newCSS, function (err) {
        if (err) {
            return console.log('写入index.css文件失败!', err.message);
        }
        console.log('写入index.css文件成功!');
    })
}


// 4.1定义处理js样式方法
function resolveJS(htmlStr) {
    // 4.2使用正则提取需要的内容
    const r2 = regScript.exec(htmlStr)
    // console.log(r2);
    // 4.3将提取出来的样式字符串,进行字符串的replace替换操作
    const newJS = r2[0].replace('<script>', '').replace('</script>', '')
    // console.log(newJS);
    // 4.4调用fs.writeFile()方法,将提取的样式,写入 index.js文件里面
    fs.writeFile(path.join(__dirname, 'index.js'), newJS, function (err2) {
        if (err2) {
            return console.log('写入index.js文件失败!', err2.message);
        }
        console.log('写入index.js文件成功!');
    })
}

// 5.1定义处理html样式方法
function resolveHTML(htmlStr) {
    // 5.2使用字符串的replace方法,把内嵌的 <style> 和 <script> 标签,替换为外联的 <link> 和 <script> 标签
    const newHTML = htmlStr
        .replace(regStyle, '<link rel="stylesheet" href="./index.css">')
        .replace(regScript, '<script src="./index.js"></script>')
    // 5.3将替换完成后的html代码,写入到index.html文件中
    fs.writeFile(path.join(__dirname, 'index.html'), newHTML, err3 => {
        if (err3) {
            return console.log('写入index.html文件失败!', err3.message);
        }
        console.log('写入index.html文件成功!');
    })
}

这个案例的意思就是 一个html文件里有html、js、css代码  通过nodejs把这些代码分离成三个文件 后缀分别为html 、js、css。注意,写入代码时,如果找不到该地址,则会自动新建文件。

在这里我把他们写在同一个目录下了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值