首页 > 编程语言 >【Node.JS 练习】时钟案例

【Node.JS 练习】时钟案例

时间:2022-10-29 16:38:49浏览次数:47  
标签:Node index const log err JS html console 时钟



目录

​​案例要求​​

​​实现​​

​​步骤​​

​​创建 正则表达式​​

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

​​自定义resolve方法​​

​​css​​

​​ js​​

​​ html​​


案例要求

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

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

<style>
body {

background-image: linear-gradient(to bottom right, rgb(157, 30, 18), rgb(242, 197, 1));
background-attachment: fixed;
}

.container {
width: 50%;
height: 50vh;
background-color: rgb(255, 255, 255);
opacity: 0.2;
position: relative;
margin: 0 auto;
top: 25vh;
}

.container div {
line-height: 150px;
text-align: center;
font-size: 50px;
width: 50%;
height: 150px;
display: inline-block;
position: absolute;
inset: 0;
margin: auto;
}
</style>
</head>

<body>

</body>
<div class="container">
<div>
<span>
11:11:00
</span>
<h2>index</h2>
</div>
</div>

<script>

</script>

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

实现

步骤

  1. 创建两个正则表达式,分别用来匹配<style>和<script>标签。
  2. 使用fs模块,读取需要被处理的html文件。
  3. 自定义resolveCSS方法,来写入index.css样式文件
  4. 自定义resolveJS方法,来写入index.js脚本文件
  5. 自定义resolveHTML方法,来写入index.html文件

创建 正则表达式

const fs = require('fs');
const path = require('path');
//定义正则表达式
const restyle = /<style>[\s\S]*<\/style>/
const rescript = /<script>[\s\S]*<\/script>/

引入fs模块和路径模块。

用正则表达式来匹配style标签,包含内部所有的空白和非空白字符,数量是任意个。

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

fs.readFile(path.join(__dirname, '/index.Html'), 'utf-8', function (err, data) {
if (err) {
console.log('读取文件失败' + err);
} else {
console.log('读取文件成功');
}

resolveCss(data);
})

自定义resolve方法

css

function resolveCss(cssStr) {
//正则匹配css
const r1 = restyle.exec(cssStr);
//替换掉非必要标签
const newCss = r1[0].replace('<style>', '').replace('</style>', '');
//将提取内容写入到clock目录中一个index.css中
fs.writeFile(path.join(__dirname, '/clock/index.css'), newCss, function (err) {
if (err) {
return console.log('导入css样式失败' + err);
} else {
console.log('写入样式成功');
}

})

}

【Node.JS 练习】时钟案例_node.js_02

生成了一个css文件

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

 js

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

}

 html

function resolveHtml(htmlStr) {
const newHtml = htmlStr.replace(restyle, '<link rel="stylesheet" href="./index.css"/>')
.replace(rescript, '<script src = "./index.js"></script>')
fs.writeFile(path.join(__dirname, '/clock/index.html'), newHtml, function (err) {
if (err) {
console.log('导入html文件失败' + err);
} else {
console.log('导入成功');
}
})
}

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

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

 

 

标签:Node,index,const,log,err,JS,html,console,时钟
From: https://blog.51cto.com/u_15830125/5806232

相关文章

  • 【Node.JS 】http的概念及作用
    什么是http模块在网络节点中,负责消费资源的电脑,叫做客户端,负责对外提供网络资源的电脑,叫做服务器。http模块是Node.js官方提供的,用来创建web服务器的模块,通过http模块提供的......
  • 【Node.JS 练习】考试成绩整理
     目标整理前的数据格式  整理后的数据格式 实现思路导入需要的fs文件系统模块使用fs.readFile()方法,读取素材目录下的成绩.txt文件。判断文件是否读取失败。文件读取成功......
  • 【Node.JS】buffer类缓冲区
    目录​​简介​​​​创建Buffer类​​​​使用Buffer类​​​​例 ​​​​直接使用buffer类​​简介node.js的开发语言就是js,javascript语言自身只有字符串数据类型,没有......
  • 【Node.JS】事件的绑定与触发
    目录​​简介​​​​绑定事件​​​​on()​​​​addListener()​​​​ once()​​​​监听事件emit()​​​​ 传参​​​​ 删除事件​​​​removeListener()​​​​ remov......
  • 【Node.JS】写入文件内容
    fs.writeFile()语法格式fs.writeFile(filepath,data[,options],callback)参数一:必选参数,为被写入文件的路径,字符串格式参数二:必选参数,表示写入的内容。参数三:可选参数,表示以什......
  • js-基础排序实现(冒泡排序,快速排序,选择排序,插入排序,希尔排序,归并排序,堆排序)
    冒泡排序:两个指针循环,遇到不合适就交换,直到将符合要求的浮到边界functionbubbleSort(list){ for(leti=0;i<list.length;i++){ for(letj=0;j<list.length-i-1;j++)......
  • 【JS ES6】use strict 严格模式
    目录​​什么是严格模式​​​​启用严格模式​​​​严格模式中的变化​​由于JavaScript语法不够严谨,一直被人们所诟病,例如在使用一个变量时,可以不使用var关键字来提......
  • [JS ES6]传值和传址
     目录​​传值​​​​什么是传值​​​​ 例 ​​​​传址​​​​什么是传址​​​​例 ​​传值什么是传值leta=1;letb=a;console.log(a,b);//11 传值是......
  • js-合法URL
    常用正则符号. 查找单个字符,除了换行和行结束符\w 查找单词字符\W 查找非单词字符\d 查找数字\D 查找非数字字符\s 查找空白字符\S 查找非空白字符\b 匹配单词边......
  • jsp 中 out 输出流 和 response.getwriter()输出流
    1)jsp中out和response的writer的区别演示<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C/......