- 执行npm install bcryptjs --save安装依赖包,使用bcryptjs进行加密
- user.js api中导入bcryptjs
const Router = require('koa-router')
const {RegisterValidator} = require('../../validators/validator')
const {User} = require('../../models/user')
const bcrypt = require('bcryptjs')
const router = new Router({
prefix: '/v1/user'
})
// 注册 新增
router.post('/register', async (ctx)=>{
// 思维路径
// 接收参数, validator校验
// email, password1,password2,nickname
const v = await new RegisterValidator().validate(ctx)
const salt = bcrypt.genSaltSync(10)
const psw = bcrypt.hashSync(v.get('body.password1'), salt)
// 获取到传递的数据
const user = {
nickname: v.get('body.nickname'),
password: psw,
email: v.get('body.email')
}
// 关联到数据库
const r = await User.create(user)
})
module.exports = router
使用模型改写加