// 导入模块
const http = require('http') // 创建web服务器实例
const server = http.createServer() // 为服务器实例绑定request事件,监听客户端请求
server.on('request', function (req, resp) {
console.log("Someone visit our web server.");
}) // 启动服务器
server.listen(8080, function () {
console.log("server running at http://120.0.0.1:8080");
})
// 导入模块
const http = require('http') // 创建web服务器实例
const server = http.createServer() server.on('request', (req) => {
const url = req.url
const method = req.method
const str = `Your request url is ${url},and request method is ${method}`
console.log(str);
}) // 启动服务器
server.listen(8080, function () {
console.log("server running at http://120.0.0.1:8080");
})
// 导入模块
const http = require('http') // 创建web服务器实例
const server = http.createServer() server.on('request', (req, resp) => {
// 设置编码格式
resp.setHeader('Content-Type', 'text/html; charset=utf-8') const url = req.url
const method = req.method
const str = `你请求的url是 ${url},请求方法是 ${method}`
console.log(str); // 调用resp.end方法,响应信息
resp.end(str)
}) // 启动服务器
server.listen(8080, function () {
console.log("server running at http://120.0.0.1:8080");
})
const fs = require('fs')
const path = require('path') // 定义正则表达式,分别匹配style和script
const regStyle = /<style>[\s\S]*<\/style>/
const regScript = /<script>[\s\S]*<\/script>/ fs.readFile (path.join(__dirname, './clock/index.html'), 'utf-8', function(err, dataStr) {
if (err) {
return console.log("读取HTML文件失败" + err.message);
} resolveCSS(dataStr)
resolveJs(dataStr)
resolveHtml(dataStr)
}) // 定义处理css样式的方法
function resolveCSS(cssStr) {
// 使用正则提取需要的内容
const r1 = regStyle.exec(cssStr) // 将提取出来的样式字符串,进行字符串的replace替换操作
const newCss = r1[0].replace('<style>', '').replace('</style>', '') // 将提取的样式,存储到index.css文件中
fs.write(path.join(__dirname, "./clock/index.css"), newCss, function(err) {
if (err) {
return console.log("css文件写入失败" + err.message);
}
console.log("css文件写入成功");
})
} // 定义处理js样式的方法
function resolveJs(JsStr) {
// 使用正则提取需要的内容
const r2 = regScript.exec(JsStr) // 将提取出来的样式字符串,进行字符串的replace替换操作
const newJs = r2[0].replace('<script>', '').replace('</script>', '') // 将提取的样式,存储到index.css文件中
fs.write(path.join(__dirname, "./clock/index.js"), newJs, function(err) {
if (err) {
return console.log("js文件写入失败" + err.message);
}
console.log("js文件写入成功");
})
} // 定义处理html文件的方法
function resolveHtml(htmlStr) {
// 将字符串调用replace方法,把内嵌的style和script替换成内联标签
const newHtml = htmlStr.replace(regStyle, '<link rel="stylesheet" href="./index.css" />')
.replace(regScript, '<script src="./index.js"></script>') // 写入html文件
fs.write(path.join(__dirname, './clock/index.html'), newHtml, function(err) {
if (err) {
return console.log("html文件写入失败" + err.message);
} console.log("文件写入成功");
})
}

NodeJs学习日报——day3的更多相关文章

  1. NodeJs学习日报day8——接口编写

    今天看了黑马NodeJs中关于接口编写以及跨域问题的视频

  2. NodeJs学习日报day9——操作数据库

    const mysql = require('mysql') const db = mysql.createPool({ // 数据库的ip地址 host: 'localhost', user: 'r ...

  3. NodeJs学习日报day7——简单中间件

    const express = require('express') const app = express() const mw = function(req, resp, next) { cons ...

  4. NodeJs学习日报day6——路由模块

    const express = require('express') const app = express() app.get('/user', function(req, resp) { resp ...

  5. NodeJs学习日报day5——导入模块

    const { match } = require("assert") function dateFormat(dataStr) { const dt = new Date(dat ...

  6. NodeJS学习日报day4——模块化

    // console.log(module); // 执行顺序不同,结果也不同 // module.exports = { // name : 'Cra2iTeT', // hi() { // con ...

  7. Nodejs学习路线图

    前言 用Nodejs已经1年有余,陆陆续续写了48篇关于Nodejs的博客文章,用过的包有上百个.和所有人一样,我也从Web开发开始,然后到包管 理,再到应用系统的开发,最后开源自己的Nodejs项目 ...

  8. Nodejs学习笔记(四)——支持Mongodb

    前言:回顾前面零零碎碎写的三篇挂着Nodejs学习笔记的文章,着实有点名不副实,当然,这篇可能还是要继续走着离主线越走越远的路子,从简短的介绍什么是Nodejs,到如何寻找一个可以调试的Nodejs ...

  9. Nodejs学习笔记(三)——一张图看懂Nodejs建站

    前言:一条线,竖着放,如果做不到精进至深,那就旋转90°,至少也图个幅度宽广. 通俗解释上面的胡言乱语:还没学会爬,就学起走了?! 继上篇<Nodejs学习笔记(二)——Eclipse中运行调试 ...

随机推荐

  1. ubuntu21.10搭建jenkins和gitlab自动化部署环境

    镜像下载.域名解析.时间同步请点击阿里云开源镜像站 前置环境: vmware pro 16 + ubuntu21.10 安装gitlab 搭建ssh远程 打开终端 sudo apt-get insta ...

  2. maltego的基本使用

    首次使用maltego需要注册 注册网站:https://www.paterva.com/web7/buy/maltego-clients/maltego-ce.php 填写好点注册就行了 1. 打开 ...

  3. 【Ansible】ansible容器学习环境搭建

    想要学习ansible,只有一个节点肯定是不行的,而搭建虚拟机又是一件非常费时费力费资源的事情,所以通过docker 快速搭建一个容器学习环境是一个不错的选择 1. 了解ansible部署 1.1 需 ...

  4. 12.16 JAVA swing

    ------------恢复内容开始------------ 12.16JAVA swing 1.框架 JFrame>JPanel>组件JButton JTestfilled JTable ...

  5. Python -用虚拟环境保存库文件

    如果你同时负责多个 Python 项目,或者想要轻松打包某个项目及其关联的库文件,再或者你担心已安装的库之间可能有冲突,那么你可以安装一个 Python 虚拟环境来分而治之.当一个 Python 库不 ...

  6. 为什么操作 DOM 慢?

    DOM本身是一个js对象, 操作这个对象本身不慢, 但是操作后触发了浏览器的行为, 如repaint和reflow等浏览器行为, 使其变慢

  7. v-for key值?

    不写key值会报warning, 和react的array渲染类似. 根据diff算法, 修改数组后, 写key值会复用, 不写会重新生成, 造成性能浪费或某些不必要的错误

  8. @Autowired 注解?

    @Autowired 注解提供了更细粒度的控制,包括在何处以及如何完成自动装配.它的用法和@Required一样,修饰setter方法.构造器.属性或者具有任意名称和/或多个参数的PN方法.

  9. 什么是springboot?为什么要用springboot?

    一.什么是springboot? Springboot是spring发展到一定程度的产物,但并不是spring的替代品,springboot是为了让程序员更好的使用spring.Spring随着发展逐 ...

  10. JDBC如何解决乱码

    只要在连接URL字符里添加参数 ?useUnicode=true&characterEncoding=utf-8 完整的URL字符串如下: 1 String url = "jdbc: ...