【发布时间】:2021-02-13 17:42:45
【问题描述】:
我正在使用 express 和 mongoDB 进行一些练习,当我定义 API 路由时,我意识到互联网上的人们使用第一种方法。但我认为第二个更容易阅读,尤其是对于更大的数据。我想知道它们之间是否存在差异或安全问题。提前致谢。
// first approach
router.route("/add").post((req, res) => {
const email = req.body.email;
const password = req.body.password;
const displayName = req.body.displayName;
const newUser = new User({ email, password, displayName });
newUser
.save()
.then(() => res.json("OK"))
.catch((err) => {
res.status(400).json(err);
});
});
// second approach
router.route("/add").post((req, res) => {
const newUser = new User({ ...req.body });
newUser
.save()
.then(() => res.json("OK"))
.catch((err) => {
res.status(400).json(err);
});
});
【问题讨论】:
标签: node.js mongodb api express