四、mongoose的使用

mongoose

核心概念

  • schema

    约束字段/列数据

  • model模型

    对应 集合 后期用它来实现数据增删改查

简介

  1. 安装

    npm i mongoose
    
  2. schema

    英文网: http://mongoosejs.com

    中文网: http://mongoosejs.net/

    作用:用来约束MongoDB文档数据(哪些字段必须,哪些字段可选的)

  3. model

    一个模型对应一个集合。后面通过模型管理集合中的数据。

使用

基本模型

// 一、导入模块
const mongoose = require("mongoose");
// 二、连接数据库
const db = mongoose.createConnection(
  "mongodb://shop2:shop2@localhost:27017/shop",
  { useNewUrlParser: true, useUnifiedTopology: true },
  (err) => {
    if (err) {
      console.log("---------------------------------------");
      console.log("数据库连接失败:", err);
      console.log("---------------------------------------");
      return;
    }
    console.log("数据库连接成功");
  }
);

// 三、设置数据模型(声明是哪个集合,限制字段个数和字段类型)
const model = db.model("api", {
  uname: { type: String, default: "神龙教主" },
  pwd: String,
  age: { type: Number },
  sex: { type: String },
});
  1. 插入一条数据

    const insertObj = new model({
        uname: "张三",
        pwd: "123",
        age: 18,
        sex: "man",
    });
    
    insertObj
        .save()
        .then((res) => {
        return res;
    })
        .catch((err) => {
        console.log("插入失败" + err);
        return false;
    });
    
    
  2. 查询数据

    model
        .find({ uname: "张三" })
        .then((res) => {
        console.log(res);
    
        return res;
    })
        .catch((err) => {
        console.log(err);
        return false;
    });
    
  3. 分页查询

    model
        .find({ uname: "张三" })
        .skip(1)
        .limit(1)
        .then((res) => {
        console.log(res);
    
        return res;
    })
        .catch((err) => {
        console.log(err);
        return false;
    });
    
    

教学管理系统接口开发

基本框架(express)

const express = require("express");
const app = express();
const port = 3000;

app.get("/", (req, res) => res.send("Hello World!"));
app.listen(port, () =>
  console.log(`Server running at  http://127.0.0.1:${port}`)
);

学生添加接口

  1. 导入body-parser模块接收前端传来的数据

  2. 定义路由

    分模块开发,将路由的方法写在/constroller/stu.js文件中。

    const stuController = require(process.cwd() + "/constroller/stu");
    app.post("/stu", stuController.create);
    

    /constroller/stu.js文件中,导入模型,调用模型中的方法完成逻辑的编写,最后导出。

    // 导入模型
    const { createModle } = require(process.cwd() + "/models/stu");
    const create = async (req, res) => {
        //   res.send("this is stu create");
        let postData = req.body;
        let rs = await createModle(postData);
        if (rs) {
            res.send({
                meta: {
                    state: 200,
                    msg: "添加成功",
                },
                data: null,
            });
        } else {
            res.send({
                meta: {
                    state: 500,
                    msg: "添加失败",
                },
                data: null,
            });
        }
    };
    
    // 导出成员
    module.exports = { create };
    

    /models/stu.js模型文件中,定义与数据库相关的操作。

    // 一、导入模块
    const mongoose = require("mongoose");
    // 二、连接数据库
    const db = mongoose.createConnection(
      "mongodb://shop2:shop2@localhost:27017/shop",
      { useNewUrlParser: true, useUnifiedTopology: true },
      (err) => {
        if (err) {
          console.log("---------------------------------------");
          console.log("数据库连接失败:", err);
          console.log("---------------------------------------");
          return;
        }
        console.log("数据库连接成功");
      }
    );
    
    // 三、设置数据模型(声明是哪个集合,限制字段个数和字段类型)
    const model = db.model("stu", {
      uname: { type: String, default: "神龙教主" },
      pwd: String,
      age: { type: Number },
      sex: { type: String },
    });
    
    const createModle = (postData) => {
      insertObj = new model(postData);
      return insertObj
        .save()
        .then((res) => {
          return res;
        })
        .catch((err) => {
          console.log(err);
          return false;
        });
    };
    
    // 四、方法
    
    module.exports = { createModle };
    
    

学生列表接口

  1. 定义路由/stu get请求

    app.get("/stu", stuController.create);
    
  2. 为控制器新增方法

    const index = (req, res) => {
        listModle().then((rs) => {
            if (rs) {
                res.send({
                    meta: {
                        state: 200,
                        msg: "查询成功",
                    },
                    data: rs,
                });
            } else {
                res.send({
                    meta: {
                        state: 500,
                        msg: "查询失败",
                    },
                    data: null,
                });
            }
        });
    };
    
  3. 修改stu模型,增加查询方法

    const listModle = () => {
      return model
        .find()
        .then((res) => {
          console.log(res);
          return res;
        })
        .catch((err) => {
          console.log(err);
          return null;
        });
    };
    

学生列表接口分页

  1. 添加路由

    app.get("/stu", stuController.index);
    
  2. 修改控制器

    const index = (req, res) => {
      let getData = req.query;
      let pagesize = parseInt(getData.pagesize);
      let skip = (parseInt(getData.pageno) - 1) * pagesize;
      listModle(skip, pagesize).then((rs) => {
        if (rs) {
          res.send({
            meta: {
              state: 200,
              msg: "查询成功",
            },
            data: rs,
          });
        } else {
          res.send({
            meta: {
              state: 500,
              msg: "查询失败",
            },
            data: null,
          });
        }
      });
    };
    
  3. 修改模型

    const listModle = (skip, limit) => {
      return model
        .find()
        .skip(skip)
        .limit(limit)
        .then((res) => {
          console.log(res);
          return res;
        })
        .catch((err) => {
          console.log(err);
          return null;
        });
    };
    

apiDoc的使用

  1. 安装模块(仅一次)

    npm install apidoc -g
    
  2. 在项目根目录创建apidoc.json文件(仅一次)

    {
    "name": "example",
    "version": "0.1.0",
    "description": "apiDoc basic example",
    "title": "Custom apiDoc browser title",
    "url" : "https://api.github.com/v1"
    }
    

    例如:

    {
     "name": "教学管理系统接口文档",
     "version": "1.0.0",
     "description": "一个非常NB的接口文档",
     "title": "Custom apiDoc browser title",
     "url" : "http://localhost:3000"
    }
    
  3. 写接口注释(N次)

    /**
     * @api {get} /user/:id Request User information
     * @apiName GetUser
     * @apiGroup User
     *
     * @apiParam {Number} id Users unique ID.
     *
     * @apiSuccess {String} firstname Firstname of the User.
     * @apiSuccess {String} lastname  Lastname of the User.
     */
    

    例如:

    /**
     * @api {get} /stu 学生模块列表
     * @apiName Add
     * @apiGroup Stu
     *
     * @apiParam {Number} pageno   当前页
     * @apiParam {Number} pagesize 每页显示条数
     *
     * @apiSuccess {String}  meta  状态码&提示信息
     * @apiSuccess {String}  data  数据
     */
    
  4. 生成接口文档(N次)

    apidoc -i ./接口注释目录 -o ./接口文档存放目录
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值