May 22, 2021 Mini Program Cloud Development Advanced
tcb-router is a lightweight class routing library based on Nodejs koa-style cloud development cloud functions that can be used to optimize the processing logic when the front end (small terminal) calls cloud functions on the service side. We can use it to integrate several cloud functions with similar functions in a cloud function, such as addition and deletion of a collection, or we can integrate some fragmented functions of the back end into a cloud function for centralized management, etc.
tcb-router is mainly used for processing logic when calling cloud functions on a small terminal, where we need to pass in the name of the cloud function to be called in name and the path to the route to be called in data, and app.router is used in the cloud side to write the processing function for the corresponding route.
Using developer tools, create a cloud function, such as a router, and then add the dependency of the latest version of tcb-router latest latest at package.json and install it with npm install:
"dependencies": {
"wx-server-sdk":"latest",
"tcb-router": "latest"
}
Then enter the following code in the index .js, where
app.use
that the middleware applies to all
app.router('user')
to middleware routed as the string 'user',
ctx.body
returns data to the small terminal
return app.serve()
:
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
const TcbRouter = require('tcb-router');
exports.main = async (event, context) => {
const app = new TcbRouter({event})
const {OPENID} = cloud.getWXContext()
app.use(async (ctx, next) => {//适用于所有的路由
ctx.data = {} //声明data为一个对象
await next();
})
app.router('user',async (ctx, next)=>{//路由为user
ctx.data.openId = OPENID
ctx.data.name = '李东bbsky'
ctx.data.interest = ["爬山","旅游","读书"]
ctx.body ={ //返回到小程序端的数据
"openid":ctx.data.openId,
"姓名":ctx.data.name,
"兴趣":ctx.data.interest
}
})
return app.serve()
}
On the smaller side, we can call the created router cloud function with an event handler or lifecycle function, and we can get the object in ctx.body returned by the cloud function router in the res object:
wx.cloud.callFunction({
name: 'router',
data: {
$url: "user", //路由为字符串user,注意属性为 $url
}
}).then(res => {
console.log(res)
})
Using tcb-router, we can also manage the collection of databases, we can integrate the add, remove, update, get, etc. of a collection (or multiple collections) into a cloud function, we can look at the following specific cases, we enter the following code in the router cloud function:
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
const TcbRouter = require('tcb-router');
const db = cloud.database()
const _ = db.command
const $ = db.command.aggregate
exports.main = async (event, context) => {
const collection= "" //数据库的名称
const app = new TcbRouter({event})
const {adddata,deleteid,updatedata,querydata,updateid,updatequery} = event
app.use(async (ctx, next) => {
ctx.data = {}
await next();
});
app.router('add',async (ctx, next)=>{
const addresult = await db.collection(collection).add({
data:adddata
})
ctx.data.addresult = addresult
ctx.body = {"添加记录的返回结果":ctx.data.addresult}
})
app.router('delete',async(ctx,next)=>{
const deleteresult = await db.collection(collection).where({
id:deleteid
}).remove()
ctx.data.deleteresult = deleteresult
ctx.body = {"删除记录的返回结果":ctx.data.deleteresult}
})
app.router('update',async(ctx,next)=>{
const getdata = await db.collection(collection).where({
id:updateid
}).update({
data:updatedata
})
ctx.data.getresult = getdata
ctx.body = {"查询记录的返回结果":ctx.data.getresult}
})
app.router('get',async(ctx,next)=>{
const getdata = await db.collection(collection).where(querydata).get()
ctx.data.getresult = getdata
ctx.body = {"查询记录的返回结果":ctx.data.getresult}
})
return app.serve();
}
Then use wx.cloud.callFunction to pass in the appropriate cloud function and the corresponding
$url
and the corresponding data value in the corresponding event handler on the small terminal:
//新增一条记录
wx.cloud.callFunction({
name: 'router',//router云函数
data: {
$url: "add",
adddata:{
id:"202006031020",
title:"云数据库的最佳实践",
content:"<p>文章的富文本内容</p>",
createTime:Date.now()
}
}
}).then(res => {
console.log(res)
})
//删除一条记录
wx.cloud.callFunction({
name: 'router',
data: {
$url:"delete",
deleteid:"202006031020"
}
}).then(res => {
console.log(res)
})
//查询记录
wx.cloud.callFunction({
name: 'router',
data: {
$url:"get",
querydata:{
id:"202006031020",
}
}
}).then(res => {
console.log(res)
})
For more advanced usage of tcb-router, you can view the technical documentation: tcb-router Github address. Some instructions when using tcb-router: