📌  相关文章
📜  mongoose 发布新文档 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:28.410000             🧑  作者: Mango

代码示例1
//Router file. After doing all your imports at beginning of file
const router = express.Router();
app.use('/pathforpostrequests', router);

router.post('/', controller.create, (req, res, next) => {
  res.status(200).json(res.locals.person);
});

// Controller file. After doing all your imports at beginning of file. Person is an example mongo schema you will need to setup in a schema file.
const controller = {
  create (req, res, next) {
    const {firstName, lastName, age} = req.body;
    const newPerson = new Person ({firstName, lastName, age});
    res.locals.person = newPerson;
    newPerson.save();
    next();
  },
}