• 【Node.JS 练习】时钟案例


       往期文章

    【Node.JS 】path路径模块

    【Node.JS 练习】考试成绩整理

    【Node.JS】buffer类缓冲区

    【Node.JS】事件的绑定与触发

    【Node.JS】写入文件内容

    【Node.JS】读取文件内容


    目录

    案例要求

    实现

    步骤

    创建 正则表达式

    使用相关模块,读取需要被处理的html文件

    自定义resolve方法

    css

     js

     html


    案例要求

     将素材目录下的index.html页面,拆分成三个文件,分别是:index.css,index.js,index.html

    并且将拆分出的三个文件存放到clock目录中。

    1. <body>
    2. body>
    3. <div class="container">
    4. <div>
    5. <span>
    6. 11:11:00
    7. span>
    8. <h2>indexh2>
    9. div>
    10. div>
    11. <script>
    12. script>

    实现

    步骤

    1. 创建两个正则表达式,分别用来匹配', '');
  • //将提取内容写入到clock目录中一个index.css中
  • fs.writeFile(path.join(__dirname, '/clock/index.css'), newCss, function (err) {
  • if (err) {
  • return console.log('导入css样式失败' + err);
  • } else {
  • console.log('写入样式成功');
  • }
  • })
  • }
  • 生成了一个css文件

     js

    1. function resolveJS(jsStr) {
    2. //正则匹配js
    3. const r1 = rescript.exec(jsStr);
    4. //替换掉非必要标签
    5. const newJs = r1[0].replace('', '');
    6. //将提取内容写入到clock目录中一个index.js中
    7. fs.writeFile(path.join(__dirname, '/clock/index.js'), newJs, function (err) {
    8. if (err) {
    9. return console.log('导入js样式失败' + err);
    10. } else {
    11. console.log('写入样式成功');
    12. }
    13. })
    14. }

     html

    1. function resolveHtml(htmlStr) {
    2. const newHtml = htmlStr.replace(restyle, '')
    3. .replace(rescript, '')
    4. fs.writeFile(path.join(__dirname, '/clock/index.html'), newHtml, function (err) {
    5. if (err) {
    6. console.log('导入html文件失败' + err);
    7. } else {
    8. console.log('导入成功');
    9. }
    10. })
    11. }

  • 相关阅读:
    极智开发 | 带你实践 MQTT 协议
    【原力计划小程序】1、一篇文章深入了解小程序的学习路线(以项目驱动的方式带你学习微信小程序)
    一个新工具 nolyfill
    webpack5学习笔记
    中国电信研究院发布《5G+数字孪生赋能城市数字化应用研究报告》
    Win7安装VMware
    企业电子招标采购系统源码Spring Boot + Mybatis + Redis + Layui + 前后端分离 构建企业电子招采平台之立项流程图
    MySQL(7) Innodb 原理和日志
    解决 vscode使用Prettier格式化js文件报错:Cannot find module ‘./parser-babylon‘
    java.io.FileNotFoundException: ...my_flutter/.android/include_flutter.groovy
  • 原文地址:https://blog.csdn.net/m0_62360527/article/details/127566112