Nodejs学习之时钟案例

案例的实现步骤

  1. 创建两个正则表达式,分别用来匹配<style>和<script>标签
  2. 使用fs模块,读取需要被处理的HTML文件
  3. 自定义resolveCSS方法,来写入index.css样式文件
  4. 自定义resolveJS方法,来写入indexjs脚本文件
  5. 自定义resolveHTML方法,来写入index.html文件
  • index.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>
  • 拆分index.html文件的js文件
// 1.1 导入fs模块
const fs = require('fs')
// 1.2 导入path
const path = require('path')

// 1.3 定义正则表达式,分别匹配<style></style> 和 <script></script> 标签
const regStyle = /<style>[\s\S]*<\/style>/
const regScript = /<script>[\s\S]*<\/script>/

// 2.1 调用fs.readFile() 方法读取文件
fs.readFile(path.join(__dirname, './files/index.html'), 'utf8', function (err, dataStr) {
//    2.2读取HTML文件失败
    if (err) return console.log('读取HTML文件失败!' + err.message)
//    2.3 读取文件成功后,调用对应的三个方法,分别拆解出css, js, html文件
    resolveCSS(dataStr)
    resolveJS(dataStr)
    resolveHtml(dataStr)
})

// 3.1 定义处理css样式的方法
function resolveCSS(htmlStr) {
//    3.2 使用正则提取需要的内容
    const r1 = regStyle.exec(htmlStr)
//    3.3 将提取出来的样式字符串,进行字符串的replace替换操作
    const newCSS = r1[0].replace('<style>', '').replace('</style>', '')
//    3.4 调用fs.wirteFile()方法,将提取的样式,写入到clock目录中index.css的文件里面
    fs.writeFile(path.join(__dirname, './clock/index.css'), newCSS, function (err) {
        if (err) return console.log('写入css样式失败!' + err.message)
        console.log('写入样式文件成功!')
    })
}

// 4.1处理js脚本
function resolveJS(htmlStr) {
//4.2 通过正则,提取对于的<script></script> 标签内容
    const r2 = regScript.exec(htmlStr)
//    4.3将提取出来的内容,做进一步的处理
    const newJS = r2[0].replace('<script>', '').replace('</script>', '')
//    4.4将处理的结果,写入到clock目录中的index.js文件里面
    fs.writeFile(path.join(__dirname, './clock/index.js'), newJS, function (err) {
        if (err) return console.log('写入JavaScri脚本失败!' + err.message)
        console.log('写入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 写入index.html 这个文件
    fs.writeFile(path.join(__dirname, './clock/index.html'), newHTML, function (err) {
        if (err) return console.log('写入HTML文件失败!' + err.message)
        console.log('写入HTML页面成功!')
    })
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于想要学习 Node.js 的人来说,以下是一个常见的学习路线: 1. 入门基础知识:首先了解 JavaScript 的基础知识,包括变量、控制流、函数等等。可以通过在线教程、书籍或视频课程学习。 2. 了解后端开发概念:学习关于服务器端开发的基本概念,例如 HTTP、网络通信、数据库等。 3. 学习 Node.js 基础:通过官方文档或在线教程学习 Node.js 的基础知识,包括安装、模块系统、事件驱动编程等。 4. 掌握核心模块:了解和熟悉 Node.js 的核心模块,例如 fs、http、path 等,以便能够构建简单的服务器应用程序。 5. 学习 Express 框架:Express 是一个流行的 Node.js Web 框架,学习它可以帮助你构建更复杂的 Web 应用程序。可以阅读官方文档或参考教程来学习 Express。 6. 学习数据库集成:了解如何在 Node.js 中使用数据库,例如 MongoDB 或 MySQL。学习数据库连接、CRUD 操作等。 7. 异步编程:深入理解 Node.js 的异步编程模型,包括回调函数、Promise、async/await 等。 8. 学习 RESTful API 设计:了解如何设计和构建符合 RESTful 风格的 API。 9. 深入学习和探索:根据个人兴趣和需求,学习其他 Node.js 相关的技术、工具和框架,例如 WebSocket、GraphQL、Socket.io 等。 记住,这只是一个大致的学习路线,你可以根据自己的兴趣和需求进行调整和扩展。实践对于学习 Node.js 来说非常重要,所以尽量多做一些实际项目来巩固所学知识。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值