终端常用命令:
Win+R,cmd:打开终端node -v :查看node.js是否安装成功
cmd终端中,“cd /d 文件路径” 切换到文件目录下
文件目录页面“shift+鼠标右键”打开powershell终端,直接在文件目录下,可以不用切换文件目录
↑ :上条命令
tab:补齐文件名
esc:清空本行命令
cls :清空终端
ctrl+“~”: 打开vscode里面的终端
ctrl+c: 关闭vscode里面的终端
一、
1.1 初识Node.js
1.2 fs文件系统模块
1.3 path路径模块
纠正:获取文件扩展名:path.extname()
// 1.1 导入文件
const { log } = require('console')
const fs = require('fs')
const path = require('path')
// 1.2 给style标签,script标签 匹配正则
const regStyle = /<style>[\s\S]*<\/style>/
const regScript = /<script>[\s\S]*<\/script>/
// 2.读取文件
fs.readFile(path.join(__dirname, '../index.html'), 'utf8', function (err, dataStr) {
// 2.1文件读取失败
if (err) return console.log('读取文件失败!' + err.message)
// 2.2文件读取成功 拆解css,js,html文件
resolveCss(dataStr)
resolveJs(dataStr)
resolveHtml(dataStr)
})
// 3 定义拆解Css的函数 resolveCss
function resolveCss(htmlStr) {
// 3.1 正则匹配style
const r1 = regStyle.exec(htmlStr)
// console.log(r1)
// 3.2 替换style标签,获取style标签里面的主体代码
const newCss = r1[0].replace('<style>', '').replace('</style>', '')
// console.log(newCss)
// 3.3 创建clock文件夹,写入index.css文件
fs.writeFile(path.join(__dirname, '/clock/index.css'), newCss, function (err) {
if (err) return console.log('index.css文件写入失败!' + err.message)
console.log('index.css文件写入成功!')
})
}
// 4. 定义拆解JS的函数 resolveJs
function resolveJs(htmlStr) {
// 4.1 正则匹配script标签
const r2 = regScript.exec(htmlStr)
// console.log(r2)
// 4.2 替换,去除script标签行
const newJs = r2[0].replace('<script>', '').replace('</script>', '')
// 4.3 写入index.js文件
fs.writeFile(path.join(__dirname, './clock/index.js'), newJs, function (err) {
if (err) return console.log('index.js文件写入失败!' + err.message)
console.log('index.js文件写入成功!')
})
}
// 5.定义拆解html的函数 resolveHtml
function resolveHtml(htmlStr){
// 5.1 替换html页面中的style和script标签内容为css和js链接
const newHtml = htmlStr.replace(regStyle,'<link rel="stylesheet" href="./index.css">').replace(regScript,'<script src="./index.js"></script>')
// 5.2 写入index.html文件中
fs.writeFile(path.join(__dirname,'./clock/index.html'),newHtml,function(err){
if(err) return console.log('index.html文件写入失败!'+err.message)
console.log('index.html文件写入成功!')
})
}
1.4 http模块
const http = require('http')
const server = http.createServer()
server.on('request', (req, res) => {
// 1.获取请求的url地址
const url = req.url
// 2.设置默认的响应内容为404 Not found
let content = '<h1>404 is Not Found</h1>'
// 3.判断用户请求的是否为/或/index. html 首页
// 4.判断用户请求的是否为/about. html 关于页面
if (url === '/' || url === '/index.html') {
content = '<h1>首页</h1>'
} else if (url = '/about.html') {
content = '<h1>关于页面</h1>'
}
// 5.设置Content-Type 响应头,防止中文乱码
res.setHeader('Content-Type', 'text/html;charset=utf-8')
// 6.使用res. end() 把内容响应给客户端
res.end(content)
})
server.listen('80', () => {
console.log('server running at http://127.0.0.1')
})
二、
2.1 模块化的基本概念
模块可以替换
2.2 Node.js中的模块化
模块作用域:模块内部定义的成员,在模块外部无法被访问
2.3 npm与包
文件夹名可以有空格,但是不能是中文
2.4 模块的加载机制
三、Express
3.1 初识Express
3.2 Express路由
3.3 Express中间件
const express = require('express')
const app = express()
// 导入Node.js内置模块querystring,解析字符串
const qs = require('querystring')
// 解析表单的中间件
app.use((req, res, next) => {
// 中间件的业务逻辑
// 1.监听req的data事件
// str存储客户端发送过来的数据
let str = ''
// 给req绑定data事件,监听客户端发送的数据,用chunk接受。然后用str拼接起来
req.on('data', (chunk) => {
str += chunk
})
// 监听req的end事件,获取到完整的客户端数据后进行处理
req.on('end', () => {
// str中存放的时完整的客户端数据
// console.log(str)
// TODO:把字符串形式的数据格式 解析成对象格式
// 调用querystring.parse()函数,识别字符串,并解析成对象
const body = qs.parse(str)
// 将上游的数据挂载到req上,下游的路由也可以访问req挂载的数据:req.body
req.body = body
// 中间件不要忘了next()
next()
})
})
// 创建路由
app.post('/user', (req, res) => {
res.send(req.body)
})
app.listen(80, () => {
console.log('app server running at http://127.0.0.1')
})
3.4 使用Express写接口
四、数据库MySQL与身份认证
4.1 数据库的基本概念
4.2 安装并配置MySQL
4.3 MySQL的基本使用
4.4 在项目中操作MySQL
注意:执行delete 语句之后,结果也是一个对象, 也会包含affectedRows 属性
4.5 前后端的身份认证
六、项目
# 【node.js】 解决[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
- if语句中的res.send前必须有return
- 检查db.query()最后的res.send()正确位置
【node.js】报错Cannot mix different versions of joi schemas解决方法
使用第三方包@hapi/joi 定义表单验证规则,然后利用postman检测到返回错误为:
Cannot mix different versions of joi schemas
报错原因:
@hapi/joi 第三方包不可用,需要下载其它版本;
解决办法:
运行如下命令重新安装第三方包
npm i joi
将导入的@hapi/joi 更改为 joi
将
const joi = require("@hapi/joi")
改为:
const joi = require("joi")