Node (一) 增删改查

express + mongoose,基础增删改查

let User = require('../models/User');  // 数据库
router.post('/user/search', (req, res, next) => {
    User.find().then((userInfo) => {   // findOne 查一个
        responseData.list = userInfo;
        responseData.code = "200"
        res.json(responseData);
    })
});

// save
let user = new User(req.body);   // object
user.save().then(() => {
        responseData.msg = "新增成功"
        res.json(responseData);
 });
 // create
 User.create({username:req.body.username}).then(add=>{
                    responseData.msg = "新增成功"
                    res.json(responseData);
            })

User.update({ username: req.body.username }, { signature: req.body.signature }).then(() => { // 修改依凭,修改内容
            responseData.msg = "修改成功"
            res.json(responseData);
    })

User.remove({ username: req.body.username }).then(() => {
            responseData.msg = "删除成功"
            res.json(responseData);
    })
axios  拦截多余的config等  main.js ,只留data
    axios.interceptors.response.use(res => {        
        return res.data
    })

你可能感兴趣的:(node)