引入crypto
模块
const crypto = require('crypto')
const d5 = (str)=>{
return crypto.createHash('md5').update('dy'+ str).digest('hex')
}
module.exports = d5
在model
中使用
const md5 = require('../util/md5')
password:{
type: String,
required: true,
set: val => md5(val)
},
在controller
中需要将返回的用户信息中的密码删除
exports.register = async (req,res) => {
console.log(req.method);
const userModel = new User(req.body)
const dbBack = await userModel.save()
const user = dbBack.toJSON()
delete user.password
res.status(201).json(user)
}