NodeJs学习日报——day3
// 导入模块
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的更多相关文章
- NodeJs学习日报day8——接口编写
今天看了黑马NodeJs中关于接口编写以及跨域问题的视频
- NodeJs学习日报day9——操作数据库
const mysql = require('mysql') const db = mysql.createPool({ // 数据库的ip地址 host: 'localhost', user: 'r ...
- NodeJs学习日报day7——简单中间件
const express = require('express') const app = express() const mw = function(req, resp, next) { cons ...
- NodeJs学习日报day6——路由模块
const express = require('express') const app = express() app.get('/user', function(req, resp) { resp ...
- NodeJs学习日报day5——导入模块
const { match } = require("assert") function dateFormat(dataStr) { const dt = new Date(dat ...
- NodeJS学习日报day4——模块化
// console.log(module); // 执行顺序不同,结果也不同 // module.exports = { // name : 'Cra2iTeT', // hi() { // con ...
- Nodejs学习路线图
前言 用Nodejs已经1年有余,陆陆续续写了48篇关于Nodejs的博客文章,用过的包有上百个.和所有人一样,我也从Web开发开始,然后到包管 理,再到应用系统的开发,最后开源自己的Nodejs项目 ...
- Nodejs学习笔记(四)——支持Mongodb
前言:回顾前面零零碎碎写的三篇挂着Nodejs学习笔记的文章,着实有点名不副实,当然,这篇可能还是要继续走着离主线越走越远的路子,从简短的介绍什么是Nodejs,到如何寻找一个可以调试的Nodejs ...
- Nodejs学习笔记(三)——一张图看懂Nodejs建站
前言:一条线,竖着放,如果做不到精进至深,那就旋转90°,至少也图个幅度宽广. 通俗解释上面的胡言乱语:还没学会爬,就学起走了?! 继上篇<Nodejs学习笔记(二)——Eclipse中运行调试 ...
随机推荐
- 字符串的高级应用-char a[100] = "1+2=;3-2=;2*5=;8/4=;" 得到char a[100] ="1+2=3;3-2=1;2*5=10;8/4=2;"
1 #include<stdio.h> 2 #include<string.h> 3 4 int main() 5 { 6 char a[100] = "1+2=;3 ...
- Linux移植到自己的开发板(二)UBOOT和Linux
@ 目录 一.uboot跳转到Linux 二. Linux内核启动之解压阶段 三. Linux内核启动之汇编阶段 插曲:关于Kconfig和Makefile 四. Linux内核启动之C语言阶段 五. ...
- centos配置ssh服务并简单测试
最近在做计算机集群方面的东西,简单弄了一下ssh服务. 首先把前提情况介绍一下: 1.我是用的虚拟机先模拟的,也不是没有真机,就是跑来跑去麻烦. 2.装了三个相同配置的centos虚拟机,详细参数就不 ...
- error C4996: 'std::_Copy_impl'
以下代码段在VS2008编译可以通过,只是会提示不安全: std::vector<unsigned char> fileData ="asdfsfsfsfsdf";// ...
- pytorch方面
(113条消息) Pytorch基础:Torch.mul.Torch.mm与Torch.matmul的异同_名字填充中的博客-CSDN博客_pytorch torch.mul (111条消息) pyt ...
- dev编译器兼容设置及字符串的识别问题
#include<bits/stdc++.h> using namespace std; bool cmp(char a,char b) { return a>b; } //int ...
- HTTP1.0和HTTP1.1和HTTP2.0的区别
1 HTTP1.0和HTTP1.1的区别1.1 长连接(Persistent Connection) HTTP1.1支持长连接和请求的流水线处理,在一个TCP连接上可以传送多个HTTP请求 ...
- Kafka01--Kafka生产者使用方式
Kafka之--生产者入门 前言: Kafka诞生至今,产生两个版本的生产者客户端:1是早期基于scala语言编写的客户端:2是随着Java用户的广泛涌入,kafka0.9版本开始退出Java版本的客 ...
- vue异步组件?
为了简化,Vue 允许你以一个工厂函数的方式定义你的组件,这个工厂函数会异步解析你的组件定义.Vue 只有在这个组件需要被渲染的时候才会触发该工厂函数,且会把结果缓存起来供未来重渲染Vue.compo ...
- 服务端处理 Watcher 实现 ?
1.服务端接收 Watcher 并存储 接收到客户端请求,处理请求判断是否需要注册 Watcher,需要的话将数据节点 的节点路径和 ServerCnxn(ServerCnxn 代表一个客户端和服务端 ...