• 【Node.js】时钟案例---将一个网页分别拆分成css、html和js文件


    案例的实现步骤:

    1. 创建两个正则表达式,分别匹配
  • const regStyle =/','')
  • //调用fs.writeFile() 将提取的样式 写入到clock目录下的index.css文件里面
  • fs.writeFile(path.join(__dirname,'./clock/index.css'),newCSS,function(err){
  • if(err) return console.log('写入失败',+ err.message)
  • console.log('写入css成功')
  • })
  • }
  • 第四步

    1. //处理js文件
    2. function resolveJS(htmlStr){
    3. //使用正则提取需要的内容
    4. const r2 = regScript.exec(htmlStr)
    5. //将提取出来的样式字符串 进行字符串的replace替换操作
    6. const newJS= r2[0].replace('','')
    7. //调用fs.writeFile() 将提取的样式 写入到clock目录下的index.js文件里面
    8. fs.writeFile(path.join(__dirname,'./clock/index.js'),newJS,function(err){
    9. if(err) return console.log('写入失败',+ err.message)
    10. console.log('写入js成功')
    11. })
    12. }

    第五步

    1. //处理html文件
    2. function resolveHTML(htmlStr){
    3. //使用字符串的replace方法 把内联的标签 替换成外联的')
    4. //调用fs.writeFile() 将提取的html代码 写入到index.html文件中
    5. fs.writeFile(path.join(__dirname,'./clock/index.html'),newHTML,function(err){
    6. if(err) return console.log('写入失败',+ err.message)
    7. console.log('写入html成功')
    8. })
    9. }

    最后写入成功

     

     

    注意点

    1、fs.writeFile()方法只能用来创建文件 不能用来创建路径   就是首先要先创建好文件夹,然后才可以成功的吧文件写入

    2、重复调用fs.writeFile()写入用一个文件,新写入的内容会覆盖之前的旧内容

  • 相关阅读:
    CF464E The Classic Problem
    [附源码]Python计算机毕业设计SSM教师信息采集系统(程序+LW)
    Linux命令记载
    Net6 EFcore框架介绍
    idea在controller或者service使用ctrl+alt+b进入方法后,如何返回到 进入前的那一层
    初识Layering Sequence
    LeetCode26——删除有序数组中的重复项
    SpringCloud 下 MultipartFile 序列化(JSON)出错的解决方案
    Python编程基础:实验3——字典及集合的使用
    Ansible 连接受控端sudo超时
  • 原文地址:https://blog.csdn.net/qiaoyangla/article/details/126189415