我正在尝试与现有用户一起登录,但它显示无法 POST/api/signin
。注册过程也是如此,我交叉检查了登录路线,但找不到问题发生的具体位置。
登录代码:
exports.signin = (req,res) => {
User.findOne({email:req.body.email})
.exec((error,user) => {
if(error) return res.status(400).json({error});
if(user){
if(user.authenticate(req.body.password)){
const token = jwt.sign({_id: user._id},process.env.JWT_SECRET,{expiresIn:'1h'})
const {firstName,lastName,email,role,fullName} = user;
res.status(200).json({
token,
user:{
firstName,lastName,email,role,fullName
}
})
} else {
return res.status(400).json({
message:"Invalid Password"
})
}
}else{
return res.status(400).json({
message:"Something went wrong!"
})
}
})
}
路由器:router.get('/signin',signin)
应用程序:app.use('/api',authRoutes);
尝试使用以下命令调用 postman : localhost:2000/api/signin
但不起作用。
相同的注册路由模式有效 localhost:2000/api/signup
最佳答案
看起来您正在执行 post
请求,而在 router
中您正在处理 get
。将其更改为:
router.post('/signin',signin)
关于javascript - 无法使用 NodeJS 登录生成 jwt token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70374325/