11-29
// 添加 删除
// 导出:
const tagModel = require("../mongodb");
module.exports = function (router) {
router.get("/tag", async (ctx) => {
const data = await tagModel.find({});//await 异步 转换为同步
ctx.body = {
status: 200,
data: data,
msg: ""
}
})
// 添加:
router.post("/tag", async ctx => {
await tagModel.create(ctx.request.body);
ctx.body = {
status: 200,
data: null,
msg: ""
}
})
}
/*
json 标准格式
{
status:200,
data:{},
msg:""
}
*/
// npm install mongoose --save 安装
// 引入:
const mongoose = require("mongoose");//1.安装mongoose
mongoose.connect("mongodb://127.0.0.1:27017/test")//2.建立连接
// 创建一个模型 tagSchema
const tagSchema = new mongoose.Schema({
//text: String,
text: {
type: String,
minlength: 2,
maxlength: 12
}//3.约束
})
const tagModel = new mongoose.model("tag", tagSchema);
// tagMOdel.
// 导出:
module.exports = tagModel;
// module.exports = userModel;
// 调用函数
// const arr = [{ text: "HTML" }, { text: "SQL" }, { text: "NOdeJS" }]
// tagModel.insertMany(arr, function (err, docs) {
// console.log(err);
// console.log(docs);
// });
// 导出:
module.exports = function(router) {
router.get("/user", async ctx => {
ctx.body = "user";
})
}
// 导出:
module.exports = function(router){
router.get("/content",async ctx=>{
ctx.body = "content";
})
}
POST http://localhost:3000/tag
Content-Type: application/json
{
"text":"张三"
}