md5密码加密

2023-03-22  本文已影响0人  奈何明月照沟渠

引入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)
}
上一篇下一篇

猜你喜欢

热点阅读