核心概念
基本模型
// 一、导入模块
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;
});
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;
});
};
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"
}
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 ./接口文档存放目录