nodejs 添加删除2


tag模块内容的查询、添加和删除

// 添加   删除
const tagModel = require("../mong0");
const { success, fail } = require("../routers/toast")

//参数获取
/*
    get    ctx.query
    post   ctx.request.body
    delete  ctx.request.body
    put    ctx.query    ctx.request.body
*/

module.exports = function (router){    // 导出函数,不然就是普通函数
    // 查询
    router.get("/tag", async (ctx) => {
        try{
            const data = await tagModel.find({});
            return success(ctx, data);
        } catch (error) {
            return fail(ctx, error)
        }
    })

    // 添加
    router.post("/tag", async ctx => {
        try{
            const data = await tagModel.create(ctx.request.body);
            return success(ctx, data);
        } catch (error) {
            return fail(ctx, error)
        }
    })

    //删除
    router.delete("/tag", async ctx=>{
        try{
            const data = await tagModel.deleteOne(ctx.request.body);
            return success(ctx, data);
        } catch (error) {
            return fail(ctx, error)
        }
    })
}

content与user模块内容的查询、添加和删除与tag模块的写法一样

toast.js

module.exports={
    success:function (ctx,data = null){
        ctx.body = {
            status:200,
            data:null,
            msg:""
        }
    },
    fail:function(cxt, msg){
        ctx.body = {
            status:200,
            data:null,
            msg:msg.message || msg
        }
    }
}

mongodb  创建模型

Schema 模型
使用模型,创建约束

const tagSchema = new mongoose.Schema({
   // text: String,
    text:{
        type:String,
        minlength:2,
        maxlength:12
    }
})

const contentSchema = new mongoose.Schema({
     title:{
         type:String,
         minlength:6,
         maxlength:12
     },
     content:{
        type:String,
        minlength:10,
        maxlength:50
     },
     top:{
        type:bool
     }
 })

 const userSchema = new mongoose.Schema({
    name:{
        type:String,
        minlength:2,
        maxlength:12
    },
    mima:{
        type:Number,
        minlength:6,
        maxlength:12
    },
    email:{
        type:String,
        minlength:6,
        maxlength:20
    },
    huzu:{
        type:String,
        minlength:6,
        maxlength:12
    }
})
创建模型   tagModel.
const tagModel = new mongoose.model("tag",tagSchema); 
const contentSchema = new mongoose.model("content",contentSchema); 
const userSchema = new mongoose.model("user",userSchema); 

test   查询、删除和添加

tag模块

### 查询
GET http://localhost:5500/tag

### 添加
POST http://localhost:5500/tag
Content-Type: application/json


# content
# id=1001&name=张三   表单方式
# json 请求体和请求地址之间加空格
{
    "text":"张三"
}

### 删除
DELETE http://localhost:5500/tag HTTP/1.1
Content-Type: application/json

{
    "id":"1"
}

content

### 查询
GET http://localhost:5500/content

### 添加
POST http://localhost:5500/content
Content-Type: application/json

{
    "title":"gggfddgg",
    "content":"易烊千玺易烊千玺易烊千玺",
    "top":false
}

### 删除
DELETE http://localhost:5500/content HTTP/1.1
Content-Type: application/json

{
    "id":"61a4861942eaac3c5c6ef5bf"
}

user模块

### 查询
GET http://localhost:5500/user

### 添加
POST http://localhost:5500/user
Content-Type: application/json

{
    "name":"易小千",
    "mima":"11282276",
    "email":"3011865496@qq.com",
    "huzu":"限制会员"
}

### 删除
DELETE http://localhost:5500/user HTTP/1.1
Content-Type: application/json

{
    "id":"61a4861942eaac3c5c6ef5bf"
}