nodejs+mongodb+express API快速生成node
安裝git
$ npm install duzq-quick-mongo
github
創建mongodb數據模型mongodb
const mongoose = require("../utils/mongodb") const dayjs = require("dayjs") // User模型 const UserSchema = new mongoose.Schema({ id:{type:String, default: dayjs().unix()}, name:String, pwd:{type: String,required:true, set(val){ // 密碼加密 return require("bcrypt").hashSync(val,10) }}, mobile:{type: String, required:true}, createTime:String, updateTime:String, },{ timestamps: { createdAt: 'createTime', updatedAt: 'updateTime' } }) const User = mongoose.model("User",UserSchema) // export module.exports = User;
初始化控制器express
const {Controller} = require("duzq-quick-mongo") const user = new Controller( require("../models/User"))
添加路由npm
router.post("/add", user.add) router.post("/getItem", user.getItem) router.post("/delete", user.delete) router.post("/update", user.update) router.post("/list", user.list) router.post("/search", user.search)
恭喜你。json
實現了User模塊的增刪改查的功能。mongoose
請求參數post
{
"name": "dzq", "mobile": "13800138000", "pwd": "123456" }
返回結果ui
{
"code": 200, "msg": "success", "data": { "id": "1608954581", "_id": "5fe6b2f1eb030db3f5d4c1bd", "name": "dzq", "mobile": "13800138000", "pwd": "$2b$10$VMgVXPNSI7TuHtIYo0vY0ufi6PgsCEc.sv1VkSl0KKkd9Hv3u4gOO", "createTime": "Sat Dec 26 2020 11:50:10 GMT+0800 (China Standard Time)", "updateTime": "Sat Dec 26 2020 11:50:10 GMT+0800 (China Standard Time)", "__v": 0 } }
請求參數
{
"id": "1608954581" }
返回結果
{
"code": 200, "msg": "success", "data": { "id": "1608954581", "_id": "5fe6b2f1eb030db3f5d4c1bd", "name": "dzq", "mobile": "13800138000", "pwd": "$2b$10$VMgVXPNSI7TuHtIYo0vY0ufi6PgsCEc.sv1VkSl0KKkd9Hv3u4gOO", "createTime": "Sat Dec 26 2020 11:50:10 GMT+0800 (China Standard Time)", "updateTime": "Sat Dec 26 2020 11:50:10 GMT+0800 (China Standard Time)", "__v": 0 } }
請求參數
{
"id": "1608954581" }
返回結果
{
"code": 200, "msg": "success", "data": { "id": "1608954581", "_id": "5fe6b2f1eb030db3f5d4c1bd", "name": "dzq", "mobile": "13800138000", "pwd": "$2b$10$VMgVXPNSI7TuHtIYo0vY0ufi6PgsCEc.sv1VkSl0KKkd9Hv3u4gOO", "createTime": "Sat Dec 26 2020 11:50:10 GMT+0800 (China Standard Time)", "updateTime": "Sat Dec 26 2020 11:50:10 GMT+0800 (China Standard Time)", "__v": 0 } }
錯誤結果
{
"code": 301, "msg": "failed" }
請求參數
{
"id": "1608954581", "updateData": { "mobile": 13800138099, "pwd": "666666" } }
請求參數
{
"pageSize": 10, "page": 1 }
返回結果
{
"code": 200, "msg": "success", "data": { "pageSize": 10, "page": 1, "total": 1, "data": [ { "id": "1608954581", "name": "dzq", "mobile": "13800138000", "pwd": "$2b$10$4otU4K9W08whZ3DFJyflBeXgxzRGrHpxlHKT940gDvvvgLmCBYT4a", "createTime": "Sat Dec 26 2020 11:57:19 GMT+0800 (China Standard Time)", "updateTime": "Sat Dec 26 2020 11:57:19 GMT+0800 (China Standard Time)" } ] } }
請求參數
默認查詢條件爲與操做,條件知足其中一條須要設置operator爲or
{
"pageSize": 10, "page": 1, "conditions": { "name": "dzq", "mobile": "18518318421" }, "operator": "or" }
返回結果
{
"code": 200, "msg": "success", "data": { "pageSize": 10, "page": 1, "total": 1, "data": [ { "id": "1608954581", "name": "dzq", "mobile": "13800138000", "pwd": "$2b$10$4otU4K9W08whZ3DFJyflBeXgxzRGrHpxlHKT940gDvvvgLmCBYT4a", "createTime": "Sat Dec 26 2020 11:57:19 GMT+0800 (China Standard Time)", "updateTime": "Sat Dec 26 2020 11:57:19 GMT+0800 (China Standard Time)" } ] } }
插件提供了一下高級自定義功能
const {Controller} = require("duzq-quick-mongo") const user = new Controller( require("../models/User")) // 設置數據查詢字段 user.projection = {"__v":0,"pwd":0,"updateTime":0,"createTime":0} // 設置成功編號 user.CODE_OK = 200 // 設置成功消息 user.MSG_OK = "請求成功" // 設置失敗編號 user.CODE_ERROR = 201 // 設置失敗消息 user.MSG_ERROR = "請求錯誤"
自定義前返回數據
{
"code": 200, "msg": "success", "data": { "id": "1608954581", "_id": "5fe6b2f1eb030db3f5d4c1bd", "name": "dzq", "mobile": "13800138000", "pwd": "$2b$10$VMgVXPNSI7TuHtIYo0vY0ufi6PgsCEc.sv1VkSl0KKkd9Hv3u4gOO", "createTime": "Sat Dec 26 2020 11:50:10 GMT+0800 (China Standard Time)", "updateTime": "Sat Dec 26 2020 11:50:10 GMT+0800 (China Standard Time)", "__v": 0 } }
自定義後獲取數據
{
"code": 200, "msg": "請求成功", "data": { "id": "1608954581", "_id": "5fe6b49feb030db3f5d4c1be", "name": "dzq", "mobile": "13800138000" } }
示例代碼:https://github.com/dzq/quick-mongo-simple
更加功能需求請提交issue: https://github.com/dzq/quick-mongo