新聞中心
tcb-router是基于Nodejs koa風格的云開發(fā)云函數(shù)輕量級的類路由庫,可以用于優(yōu)化前端(小程序端)調(diào)用服務端的云函數(shù)時的處理邏輯。我們可以使用它在一個云函數(shù)里集成多個類似功能的云函數(shù),比如針對某個集合的增刪改查;也可以把后端的一些零散功能集成到一個云函數(shù)里,便于集中管理等。

成都創(chuàng)新互聯(lián)成立與2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務公司,擁有項目成都網(wǎng)站設計、做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元淶源做網(wǎng)站,已為上家服務,為淶源各地企業(yè)和個人服務,聯(lián)系電話:028-86922220
一、tcb-router快速入門
tcb-router主要用于小程序端調(diào)用云函數(shù)時的處理邏輯,在小程序端使用wx.cloud.callFunction調(diào)用云函數(shù)時,我們需要在name里傳入要調(diào)用的云函數(shù)名稱,以及在data里傳入要調(diào)用的路由的路徑;而在云函數(shù)端使用app.router來寫對應的路由的處理函數(shù)。
使用開發(fā)者工具,創(chuàng)建一個云函數(shù),如router,然后在package.json增加tcb-router最新版latest的依賴并用npm install安裝:
"dependencies": {
"wx-server-sdk":"latest",
"tcb-router": "latest"
}
然后在index.js里輸入以下代碼,其中app.use表示該中間件適用于所有的路由,而app.router('user')則適用于路由為字符串'user'的中間件,ctx.body為返回給小程序端的數(shù)據(jù),返回的方式是通過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 ={ //返回到小程序端的數(shù)據(jù)
"openid":ctx.data.openId,
"姓名":ctx.data.name,
"興趣":ctx.data.interest
}
})
return app.serve()
}
而在小程序端,我們可以用事件處理函數(shù)或者生命周期函數(shù)來調(diào)用創(chuàng)建好的router云函數(shù),就能在res對象里獲取到云函數(shù)router返回的ctx.body里的對象了:
wx.cloud.callFunction({
name: 'router',
data: {
$url: "user", //路由為字符串user,注意屬性為 $url
}
}).then(res => {
console.log(res)
})
二、tcb-router管理數(shù)據(jù)庫的增刪改查
使用tcb-router還可以管理數(shù)據(jù)庫的集合,我們可以把一個集合(也可以是多個集合)的add、remove、update、get等集成到一個云函數(shù)里,可以看下面具體的案例,我們在router云函數(shù)里輸入以下代碼:
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= "" //數(shù)據(jù)庫的名稱
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 = {"添加記錄的返回結(jié)果":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 = {"刪除記錄的返回結(jié)果":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 = {"查詢記錄的返回結(jié)果":ctx.data.getresult}
})
app.router('get',async(ctx,next)=>{
const getdata = await db.collection(collection).where(querydata).get()
ctx.data.getresult = getdata
ctx.body = {"查詢記錄的返回結(jié)果":ctx.data.getresult}
})
return app.serve();
}
然后再在小程序端相應的事件處理函數(shù)里使用wx.cloud.callFunction傳入相應的云函數(shù)以及相應的路由$url以及傳入對應的data值即可:
//新增一條記錄
wx.cloud.callFunction({
name: 'router',//router云函數(shù)
data: {
$url: "add",
adddata:{
id:"202006031020",
title:"云數(shù)據(jù)庫的最佳實踐",
content:"文章的富文本內(nèi)容
",
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)
})
關(guān)于tcb-router更多進階用法,可以查看技術(shù)文檔:tcb-router Github地址。使用tcb-router時的一些說明:
- 通常情況下,我們不建議大家使用一個云函數(shù)來調(diào)用其他云函數(shù)這種做法,這種做法會導致云函數(shù)的執(zhí)行時間會增加很多,而且還會耗費云函數(shù)的資源,我們可以使用tcb-router來處理需要跨云函數(shù)調(diào)用的情況;
- 值得注意的是,tcb-router會把所有云函數(shù)的承載放在一個云函數(shù)里,對并發(fā)有比較高要求的云函數(shù)建議不要把用tcb-router整到一個里面。每個云函數(shù)的并發(fā)數(shù)上限為1000,這本可以每秒處理十萬級別的請求,但是如果把大量不同的云函數(shù)都集成到一個里面,尤其是一些耗時比較長的云函數(shù)會嚴重拖性能后退,而這些云函數(shù)都會共享這1000個并發(fā),所以要注意根據(jù)情況來抉擇了;
- 云函數(shù)會有一個冷啟動時間(比如十分鐘以上沒人調(diào)用這個云函數(shù),當再首次調(diào)用這個云函數(shù)會比較慢),當我們把多個功能相似、并發(fā)不會特別高(低于每秒幾千)的云函數(shù)使用tcb-router集成到一個云函數(shù)里,這樣就可以減少冷啟動的可能性了;
網(wǎng)頁名稱:創(chuàng)新互聯(lián)小程序云教程:云開發(fā) 云函數(shù)路由tcb-router
URL標題:http://m.5511xx.com/article/ccoppih.html


咨詢
建站咨詢
