路由查询/添加/删除2(总)

app文件:

//引入类
const Koa = require('koa');
const Router =require('koa-router');
const koaBody = require('koa-body');
const tagRouter =require("./routers/tag.router");
const userrouter =require("./routers/user.router");
const contentrouter =require("./routers/content.router");

//引入内部方法或属性
//const{方法或属性名} =require('koa');

//创建对象
const app = new Koa();
app.use(koaBody(
 { strict:false}
));
const router =new Router();
// tagrouter调用
tagRouter(router);
userrouter(router);
contentrouter(router);


app.use(router.routes());
app.use(router.allowedMethods({}));


// localhost:3000
app.listen(3000,()=>{
  console.log("http://localhost:3000")
});

tag文件:

// 添加 删除
const tagModel = require("../mongodb");
const {success,fail}=require("../joast");
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)
        }
    })
}

text文件:

###查询
GET http://localhost:3000/tag
###添加
POST http://localhost:3000/tag
Content-Type: application/json
# content
# id=1000&name=张三
 
{
    "text":"李四"
}
###删除
DELETE http://localhost:3000/tag HTTP/1.1
Content-Type: application/json

{ 
    "_id":"61a5c11b22beea0274420145" 
}
    

 

上一篇:动态NAT配置实验(1)


下一篇:vue项目篇(更新中)