初始化項目
創建入口文件index.js
全局安裝nodemon(實現後臺熱部署)
cnpm install nodemon -g
熱部署的啓動方式
npm run server
需要安裝的依賴
npm install express # node的服務器插件
npm install bcrypt # 給密碼進行加密的插件
npm install express
npm install express
npm install express
npm install express
npm install express
想mongo db中出入數據
//註冊接口
router.post('/register',(req,res)=>{
User.findOne({email:req.body.email})
.then((user)=>{
if(user){
return res.status(400).json('郵箱已被註冊!')
}else{
// const url = gravatar.url(req.body.email, {s: '200', r: 'pg', d: 'mm'});
const newUser = new User({
name:req.body.name,
email:req.body.email,
password:req.body.password,
identity:req.body.identity
})
//密碼加密
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(newUser.password, salt, (err, hash)=> {
if(err) throw err;
newUser.password = hash;
newUser.save()
.then(user=>res.json(user))
.catch(err=>console.log(err))
});
});
}
})
})
使用jwt(token方式進行登錄)
//jwt.sign("規則","加密名字(密鑰)","過期時間","箭頭函數")
jwt.sign(rule,'secret',{ expiresIn:3600 },(err,token)=>{}