Node接口搭建笔记(二)

视频课程地址:https://www.bilibili.com/video/av59056478
资金管理系统带权限(node/element/vue)

Node接口搭建

数据信息接口

上一篇的基础上,增加一些数据请求接口
在models下新建一个Profile.js文件创建一个新的数据模型
Profile.js

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const ProfileSchema = new Schema({
    type:{type:String},
    describe:{type:String},
    income:{type:String,required:true},
    expend:{type:String,required:true},
    cash:{type:String,required:true},
    remark:{type:String},
    date:{type:Date,default:Date.now}
});
module.exports = Profile = mongoose.model('profile', ProfileSchema);

在server.js中增加profile的配置:

const users = require("./routes/api/users.js");
const profiles = require("./routes/api/profiles.js");
......
// 使用routes
app.use("/api/users", users);
app.use("/api/profiles", profiles);

在routes/api文件夹中新建profiles.js文件,用于编写Profile相关的接口,引入一些依赖,然然后写一个测试接口:

const express = require("express");
const router = express.Router();
const passport = require("passport");
const Profile = require("../../models/Profile.js");

// @route  GET  api/profiles/test
// @desc   返回请求的json数据
// @access public接口,如果要返回token的话则是私有接口
router.get('/test', (req, res)=>{
 res.json({msg:'profile works'})
});
module.exports = router;

使用postman测试
在这里插入图片描述

添加信息接口

// @route  POST  api/profiles/add
// @desc   创建信息接口
// @access 私有接口
router.post('/add', passport.authenticate("jwt", {session:false}), 
  (req, res)=>{
     const profileFields = {};
     if(req.body.type){ profileFields.type = req.body.type; }
     if(req.body.describe){ profileFields.describe = req.body.describe; }
     if(req.body.income){ profileFields.income = req.body.income; }
     if(req.body.expend){ profileFields.expend = req.body.expend; }
     if(req.body.cash){ profileFields.cash = req.body.cash; }
     if(req.body.remark){ profileFields.remark = req.body.remark; }
     new Profile(profileFields).save().then(profile=>{
        res.json(profile);
     });
});

先使用有效账户登录后复制返回的token填入headers中,然后在body中填入相关参数
在这里插入图片描述
在数据库中可以看到添加成功的信息:
在这里插入图片描述

获取所有信息接口

// @route  GET  api/profiles
// @desc   获取所有信息
// @access 公有接口
router.get('/', passport.authenticate("jwt", {session:false}), 
  (req, res)=>{
      Profile.find().then(profile=>{
         if(!profile){
             return res.status(404).json("没有任何内容");
         }
         res.json(profile);
      }).catch(err => res.status(404).json(err));
});

测试:
在这里插入图片描述

获取单个信息接口

// @route  GET  api/profiles/:id
// @desc   获取单个信息
// @access 公有接口
router.get('/:id', passport.authenticate("jwt", {session:false}), 
  (req, res)=>{
      Profile.findOne({_id:req.params.id}).then(profile=>{
         if(!profile){
            return res.status(404).json("没有任何内容");
         }
         res.json(profile);
      }).catch(err => res.status(404).json(err));
});

编辑信息接口

// @route  POST  api/profiles/edit
// @desc   编辑信息接口
// @access 私有接口
router.post('/edit/:id', passport.authenticate("jwt", {session:false}), 
 (req, res)=>{
    const profileFields = {};
    if(req.body.type){ profileFields.type = req.body.type; }
    if(req.body.describe){ profileFields.describe = req.body.describe; }
    if(req.body.income){ profileFields.income = req.body.income; }
    if(req.body.expend){ profileFields.expend = req.body.expend; }
    if(req.body.cash){ profileFields.cash = req.body.cash; }
    if(req.body.remark){ profileFields.remark = req.body.remark; }
    Profile.findOneAndUpdate({_id:req.params.id}, 
                             {$set:profileFields},{new:true})
           .then(profile=>res.json(profile));
});

删除信息接口

// @route  delete  api/profiles/delete
// @desc   删除信息接口
// @access 私有接口
router.delete('/delete/:id', passport.authenticate("jwt", {session:false}), 
   (req, res)=>{
       Profile.findOneAndRemove({_id:req.params.id})
              .then(profile=>{
                  profile.save().then(profile => res.json(profile));
            }).catch(err => res.status(404).json("删除失败"));
});
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值