1. 程式人生 > 實用技巧 >nodejs+express+mongodb 快速介面開發

nodejs+express+mongodb 快速介面開發

nodejs+mongodb+express API快速生成

使用說明

安裝

$ npm install duzq-quick-mongo

建立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;

初始化控制器

const {Controller} = require("duzq-quick-mongo")
const user = new Controller( require("../models/User"))

新增路由

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)

恭喜你。

實現了User模組的增刪改查的功能。

介面使用

新增資料

請求引數

{
	"name": "dzq",
	"mobile": "13800138000",
	"pwd": "123456"
}

返回結果

{
	"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