![](https://img.haomeiwen.com/i4578791/e66088640e782d0d.png)
nvm
node版本管理工具,可以切换多个nodejs版本
-
安装
- mac os Homebrew
// 安装brew /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" // 安装nvm brew install nvm
- windows nvm-windows
下载地址:https://github.com/coreybutler/nvm/releases
- mac os Homebrew
-
使用
-
nvm list
: 查看当前所有node版本 -
nvm install v10.13.0
: 安装指定的版本 -
nvm uninstall
: 卸载某个版本 -
nvm use --delete-prefix 10.13.0
: 切换到指定版本
-
Http请求概述
DNS解析,建立TCP连接(3次握手🤝),发送http请求
server端接收到http请求,处理,并返回
客户端接收到返回数据,处理数据(如渲染页面)
常用的请求方式
-
GET
客户端向server端获取数据,通过querystring来传递数据,浏览器直接访问,发送get请求
const http = require('http')
const querystring = require('querystring')
const server = http.createServer((req, res) => {
console.log('method:', req.method) // GET
const url = req.url // 请求完整的url
console.log('url:', url)
req.query = querystring.parse(url.split('?')[1]) // 解析querystring
console.log('query:', req.query)
res.end(JSON.stringify(req.query)) // 返回querystring
})
server.listen(8002)
console.log('OK啦')
![](https://img.haomeiwen.com/i4578791/3267d06c44ed6527.png)
-
POST
客户端要向服务端传递数据,通过post data传递数据,使用postman或者apifox等工具模拟请求
const http = require('http')
const server = http.createServer((req, res) => {
if(req.method === 'POST') {
// 数据格式
console.log('content-type', req.headers['content-type'])
// 接收数据
let postData = ""
req.on('data', chunk => {
postData += chunk.toString()
})
req.on('end', () => {
console.log(postData)
res.end('hello world') // 在这里返回,因为是异步
})
}
})
server.listen(8000)
console.log('OK啦')
![](https://img.haomeiwen.com/i4578791/50151184c76887c4.png)
![](https://img.haomeiwen.com/i4578791/475492e66e4dc593.png)
测试项目
-
环境搭建
- nodemon监听文件变化,自动重启node
npm install --save nodemon
-
cross-env - npm (npmjs.com)
设置环境变化,兼容mac linux 和 windows
npm install --save cross-env
-
环境配置
package.json
{ "name": "blog-1", "version": "1.0.0", "description": "", "main": "bin/www.js", "dependencies": { "cross-env": "^7.0.3", "nodemon": "^3.0.1" }, "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "dev": "cross-env NODE_ENV=dev nodemon ./bin/www.js", "pro": "cross-env NODE_ENV=production nodemon ./bin/www.js" }, "keywords": [], "author": "", "license": "ISC" }
bin/www.js
// server const http = require('http') const PORT = 8000 const serverHandle = require('../app') const server = http.createServer(serverHandle) server.listen(PORT)
app.js
// 业务代码 const serverHandle = (req, res) => { // 设置返回格式为 JSON res.setHeader('Content-type', 'application/json') } module.exports = serverHandle
-
MySql
-
介绍
- web server 中最流行的关系型数据库
- 官网可免费下载,学习
- 轻量级,易学易用
-
安装
注:执行过程中需要输入root用户名的密码,要记住这个密码
-
可视化工具
mysql workbench
操作mysql的客户端,可视化操作,下载,
当然还有我们的小猫爪Navicat
大家自行下载哈~ -
操作数据库
-
建库-举个🌰🌰🌰
blog.png
-
建表-举个🌰🌰🌰
user.png
-
表操作(增删改查)
增:
sql语句前面加-- 表示注释当前查询
// 增:password是mysql的关键字需要加``转成普通的字段名 insert into user(username, `password`, 'realname')values("zhangsan","123","张三");
删
delete from user where username='lisi';
📌 通过做标记的方式删除
status列.png
-
update user set status='0' where username='lisi';
select * from user where status='1'
// 查询satus不等于0的数据
select * from users where status <> 0
![](https://img.haomeiwen.com/i4578791/717a6ccf6d7f0463.png)
改
🍬 直接执行会报错提示正在运行安全模式
update user set realname='张三6' where username='zhangsan'; //
![](https://img.haomeiwen.com/i4578791/0550345ab5947c57.png)
🍬 解决办法
// 执行成功后这条命令可以删掉啦
SET SQL_SAFE_UPDATES = 0;
🍬 再次执行更新命令
![](https://img.haomeiwen.com/i4578791/b5d7228845f3654d.png)
查
select * from user; // 整张表
select id,username from user; // 根据列查询
select * from user where username='zhangsan' and `password` = '123'; // 根据条件查询and-且or-或
select * from user where username like '%zhang%'; // 模糊查询
select * from user where password like '%1%' order by id; // 按照id进行正序排列 desc倒序排列
-
NodeJs 操作mysql
- 下载
npm install --save mysql
- index.js
const mysql = require('mysql')
// 创建连接对象
const con = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'test',
database: 'myblog'
})
// 开始连接
con.connect()
// 执行 sql 语句
const sql = 'select * from user;'
con.query(sql, (err, result) => {
if(err) {
console.error(err);
}
console.log(result);
})
// 关闭连接
con.end()
- 坑来了
// 运行
node index.js
// ✨有请咱们第一位坑闪亮登场✨
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support
authentication protocol requested by server; consider upgrading MySQL client
-
分析错误
MYSQL8.0以上的版本Node还暂时不支持
- 版本问题
解决: 可以去官网下载低版本的MySql
- 使用CMD连接
解决:
// 连接数据库 mysql -u root -p Enter password: ****** // 输入自己的密码 // 成功后输入 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456'; // 123456是自己的新密码 // 刷新权限 FLUSH PRIVILEGES;
对啦!!答应我一定!一定!一定!要改自己代码里的密码,要不然你还会看到这样的友好提示,别问我是咋知道的😭
// 嘿,兄嘚~~ Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost' (using password: YES)
![](https://img.haomeiwen.com/i4578791/b664381ecdb9ce82.jpg)
终点来啦!!!
// 运行,你就会看到!!
node index.js
![](https://img.haomeiwen.com/i4578791/69c90280d7ddfe90.png)
感谢感谢感谢这位兄台的倾囊相助啊,解决了俺的燃眉之急,大家多多支持【Node】使用Node.js连接数据库时报错客户端不支持服务器请求的身份验证协议_Genius-Sue的博客-CSDN博客
![](https://img.haomeiwen.com/i4578791/8fdeef8145d22f74.gif)
未完待续...
![](https://img.haomeiwen.com/i4578791/b8ae0bbde0bd564a.png)
网友评论