@[TOC] nodeJS express 创建服务器,并对mysql数据库进行操作 (个人学习笔记)
一、初始化
1、创建项目
1.1 创建文件夹,并进行初始化包管理文件
npm init -y
1.2 安装express
npm i express@4.17.1
1.3 在根目录文件中创建app.js文件作为整个项目的入口文件
// 导入express
const express = require('express')
// 创建服务器实例对象
const app = express()
// 启动服务器
app.listen(8888, ()=> {
console.log('服务器已启动,在8888端口运行')
})
2、配置cors跨域
2.1 安装cors中间件
npm i cors
2.2 在app.js文件中导入cors 中间件
// 导入并配置cors中间件
const cors = require('cors')
// 注册为全局
app.use(cors())
2.3 配置解析表单数据的中间件(application/x-www-form-urlencoded)
app.use(express.urlencoded({ extended: false }))
3、创建路由相关文件(一个存放路径,一个进行处理相关配置),并进行初始化
3.1、初始化用户路由模块(在router文件夹下创建user.js文件,并作为用户的路由模块)代码如下:
const express = require('express')
// 创建路由对象
const router = express.Router()
// 注册新用户
router.post('/resgiter', (req, res) => {
res.send('resgiter ok')
})
// 登录
router.post('/login', (req, res) => {
res.send('login ok')
})
module.exports = router
- 导入并使用用户路由模块(app.js中)
const userRouter = require('./router/user')
app.use('/api', userRouter)
使用postman工具进行测试,结果如下:
4、抽离用户路由模块中的处理函数
router 文件(user.js)
const express = require('express')
// 创建路由对象
const router = express.Router()
// 导入用户路由对应的处理函数模块
const user_handle = require('../rotuter_handle/user')
// 注册新用户
router.post('/resgiter', user_handle.regiter)
// 登录
router.post('/login', user_handle.login)
module.exports = router
router_handle 文件(user.js)
// 注册处理函数
exports.regiter = (req, res) => {
res.send('resgiter ok')
}
// 登录处理函数
exports.login = (req, res) => {
res.send('login ok')
}
二、与mysql数据库的连接应用
1、创建mysql数据表(可以使用Navicat数据库可视化工具)
2、安装并配置mysql模块
2.1、安装mysql 命令
npm i mysql
2.2、新建文件,并创建数据库的连接对象
// 导入mysql 模块
const mysql = require('mysql')
// 创建数据库连接对象
const db = mysql.createPool({
host: '127.0.0.1',//主机IP
user: 'root', //数据库用户名
password: '', //数据库密码
database: 'nodedb'//数据库名字
})
// 向外共享db数据库连接对象
module.exports = db
2.3、进行用户注册实践
- 检测表单是否合法
- 检测用户名是否被占用
- 对密码进行加密处理
- 插入新用户
2.3.1 检测表单是否合法
// 注册处理函数
exports.resgiter = (req, res) => {
// 获取客户端提交到服务器的用户信息
const userInfo = req.body
// 判断数据是否合法
if (!userInfo.username || !userInfo.password) {
return res.send({ status: 1, message: '用户名或密码不合法' })
}
}
测试结果
2.3.2 检测用户名是否重复(处理函数中)
- 导入数据库操作模块
const db = require('../db/index')
- 定义sql语句, 查询用户名是否被占用
const sqlStr = 'select * from ev_users where username = ?'
db.query(sqlStr, userInfo.username, (err,result) => {
// 执行mysql语句失败
if (err) {
return res.send({ status: 1, message: err.message })
}
// 判断用户名是否被占用
if (result.length > 0) {
return res.send({ status: 1, message: '用户名已被占用,请更换' })
}
// 用户名可以使用
})
2.3.3 对密码进行加密处理(处理函数中)
- 使用bcryptjs的优点:
1、加密之后的密码,无法被逆向破解
2、同一明文密码多次加密,得到的加密结果各不相同,保证了安全性、
2.3.3.1安装命令
npm i bcryptjs
使用bcryptjs
// 导入bcryptjs
const bcrypt = require('bcryptjs')
调用bcryptjs 中的hashSync()方法进行加密,并将加密后的值复制给表单password(当登录时,使用compareSync方法进行登录密码验证)
userInfo.password = bcrypt.hashSync(userInfo.password, 10)
定义插入新用户的sql语句
const sqlInsert = 'insert into ev_users set ?'
db.query(sqlInsert, { username: userInfo.username, password: userInfo.password }, (err, result) => {
if (err) {
return res.send({ status: 1, message: err.message })
}
if (result.affectedRows !== 1) {
return res.send({ status: 1, message: '注册用户失败,请稍后重试' })
}
// 注册成功
res.send({ status:0, message: '注册成功' })
})
2.4、处理函数(router_handle 》 user.js)完整代码:
// 导入数据库操作模块
const db = require('../db/index')
// 导入bcryptjs
const bcrypt = require('bcryptjs')
// 注册处理函数
exports.resgiter = (req, res) => {
// 获取客户端提交到服务器的用户信息
const userInfo = req.body
// 判断数据是否合法
if (!userInfo.username || !userInfo.password) {
return res.send({ status: 1, message: '用户名或密码不合法' })
}
// 定义sql语句, 查询用户名是否被占用
const sqlStr = 'select * from ev_users where username = ?'
db.query(sqlStr, userInfo.username, (err,result) => {
// 执行mysql语句失败
if (err) {
return res.send({ status: 1, message: err.message })
}
// 判断用户名是否被占用
if (result.length > 0) {
return res.send({ status: 1, message: '用户名已被占用,请更换' })
}
// d调用bcryptjs 中的hashSync()方法进行加密
userInfo.password = bcrypt.hashSync(userInfo.password, 10)
// 定义插入新用户的sql语句
const sqlInsert = 'insert into ev_users set ?'
db.query(sqlInsert, { username: userInfo.username, password: userInfo.password }, (err, result) => {
if (err) {
return res.send({ status: 1, message: err.message })
}
if (result.affectedRows !== 1) {
return res.send({ status: 1, message: '注册用户失败,请稍后重试' })
}
// 注册成功
res.send({ status:0, message: '注册成功' })
})
})
}
// 登录处理函数
exports.login = (req, res) => {
res.send('login ok')
}
结果如下图