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

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的鹽池網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
一、tcb-router快速入門
tcb-router主要用于小程序端調(diào)用云函數(shù)時(shí)的處理邏輯,在小程序端使用wx.cloud.callFunction調(diào)用云函數(shù)時(shí),我們需要在name里傳入要調(diào)用的云函數(shù)名稱,以及在data里傳入要調(diào)用的路由的路徑;而在云函數(shù)端使用app.router來寫對(duì)應(yīng)的路由的處理函數(shù)。
使用開發(fā)者工具,創(chuàng)建一個(gè)云函數(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為一個(gè)對(duì)象
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對(duì)象里獲取到云函數(shù)router返回的ctx.body里的對(duì)象了:
wx.cloud.callFunction({
name: 'router',
data: {
$url: "user", //路由為字符串user,注意屬性為 $url
}
}).then(res => {
console.log(res)
})
二、tcb-router管理數(shù)據(jù)庫的增刪改查
使用tcb-router還可以管理數(shù)據(jù)庫的集合,我們可以把一個(gè)集合(也可以是多個(gè)集合)的add、remove、update、get等集成到一個(gè)云函數(shù)里,可以看下面具體的案例,我們?cè)趓outer云函數(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();
}
然后再在小程序端相應(yīng)的事件處理函數(shù)里使用wx.cloud.callFunction傳入相應(yīng)的云函數(shù)以及相應(yīng)的路由$url以及傳入對(duì)應(yīng)的data值即可:
//新增一條記錄
wx.cloud.callFunction({
name: 'router',//router云函數(shù)
data: {
$url: "add",
adddata:{
id:"202006031020",
title:"云數(shù)據(jù)庫的最佳實(shí)踐",
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更多進(jìn)階用法,可以查看技術(shù)文檔:tcb-router Github地址。使用tcb-router時(shí)的一些說明:
- 通常情況下,我們不建議大家使用一個(gè)云函數(shù)來調(diào)用其他云函數(shù)這種做法,這種做法會(huì)導(dǎo)致云函數(shù)的執(zhí)行時(shí)間會(huì)增加很多,而且還會(huì)耗費(fèi)云函數(shù)的資源,我們可以使用tcb-router來處理需要跨云函數(shù)調(diào)用的情況;
- 值得注意的是,tcb-router會(huì)把所有云函數(shù)的承載放在一個(gè)云函數(shù)里,對(duì)并發(fā)有比較高要求的云函數(shù)建議不要把用tcb-router整到一個(gè)里面。每個(gè)云函數(shù)的并發(fā)數(shù)上限為1000,這本可以每秒處理十萬級(jí)別的請(qǐng)求,但是如果把大量不同的云函數(shù)都集成到一個(gè)里面,尤其是一些耗時(shí)比較長的云函數(shù)會(huì)嚴(yán)重拖性能后退,而這些云函數(shù)都會(huì)共享這1000個(gè)并發(fā),所以要注意根據(jù)情況來抉擇了;
- 云函數(shù)會(huì)有一個(gè)冷啟動(dòng)時(shí)間(比如十分鐘以上沒人調(diào)用這個(gè)云函數(shù),當(dāng)再首次調(diào)用這個(gè)云函數(shù)會(huì)比較慢),當(dāng)我們把多個(gè)功能相似、并發(fā)不會(huì)特別高(低于每秒幾千)的云函數(shù)使用tcb-router集成到一個(gè)云函數(shù)里,這樣就可以減少冷啟動(dòng)的可能性了;
網(wǎng)站欄目:創(chuàng)新互聯(lián)小程序云教程:云開發(fā)云函數(shù)路由tcb-router
轉(zhuǎn)載注明:http://fisionsoft.com.cn/article/cciohhg.html


咨詢
建站咨詢
