1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| const bcrypt = require('bcrypt');
router.post('/register', (req, res) => { console.log(req.body);
User.findOne({email: req.body.email}) .then((user) => { if(user){ return res.status(400).json({email: "邮箱已被注册"}); } else { let newUser = new User({ name:req.body.name, email:req.body.email, password: req.body.password, identity: '123213' }); bcrypt.genSalt(10, function(err, salt) { bcrypt.hash(newUser.password, salt, function(err, hash) { if (err) throw err;
newUser.password = hash; newUser.save() .then(user => res.json(user)) .catch(err => console.log(err)) }); }); } }) })
|