Node.js 带有express api的空正文,当bcryptjs
我的目标是用ExpressAPI(与angular前端一起使用)制作一个身份验证系统 几个小时前,我在创建前端时遇到了一个问题: 对代码中的密码进行哈希运算时,api引发了一些错误:Node.js 带有express api的空正文,当bcryptjs,node.js,api,express,body-parser,Node.js,Api,Express,Body Parser,我的目标是用ExpressAPI(与angular前端一起使用)制作一个身份验证系统 几个小时前,我在创建前端时遇到了一个问题: 对代码中的密码进行哈希运算时,api引发了一些错误: const name = req.body.name; const email = req.body.email; const password = bcrypt.hashSync(String(req.body.password));
const name = req.body.name;
const email = req.body.email;
const password = bcrypt.hashSync(String(req.body.password));
console.log(req.body);
usermodel.createUser([name, email, password], (err) => {
try {
if (err) return res.json({
success: false,
message: err.toString(),
});
usermodel.findUserByEmail(email, (err, user) => {
if (err) return res.json({
success: false,
message: 'Server error! '
});
console.log(user);
const expiresIn = 24 * 60 * 60;
const accessToken = jwt.sign({
id: user.id
}, config.secret, {
expiresIn: expiresIn
});
res.json({
success: true,
"user": user,
"access_token": accessToken,
"expires_in": expiresIn
});
});
} catch (error) {
console.log(error);
res.json({success: false, message:error.toString()})
}
经过几次修改后,即使我输入了一些参数,主体仍然是空的:
app.post('/ping', function(req, res) {
console.log('trigerred')
res.json({body: req.body});
});
-->未定义:(
我已经遇到了这个问题,因为我的express api不是带有postman的https,所以我使用https。
我验证了bodyparser:
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
知道为什么尸体是空的吗?谢谢
PS:这很奇怪,因为它几个小时前还在工作你能分享你的api请求吗
console.log(req.body)
如果你把它放在散列之前,控制台中的输出是什么呢?不管怎样,使用curl,或postman,或来自前端的请求,ping命令的主体={}