我有以下代码,并且有一个开发者告诉我必须验证我的请求:
router.post('/dashboard', passport.authenticate('jwt', { session: false }),
(req, res) => {
try {
let newPost = new Post({
category: req.body.category,
title: req.body.title,
photo: req.body.photo,
text: req.body.text,
author: req.body.author,
date: req.body.date
});
Post.addPost(newPost, (err, user) => {
if (err) {
res.json({success: false, msg: `Post has not been added. ${err}`})
} else {
res.json({success: true, msg: 'Post was added.'})
}
})
} catch (err) {
const error = (res, error) => {
res.status(500).json({
success: false,
message: error.message ? error.message : error
})
}
console.log('error routes/auth POST', res, error)
}
})
我通过谷歌搜索发现了解决方案,即使用请求验证库(例如express-validator
)进行验证 - 这样可以吗?
是否有原生的内置于node.js和express中用于请求验证的方法?
还是最好使用像express-validator
这样的库?