云開發(fā) 云函數(shù)路由tcb-router

2020-07-21 17:55 更新

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

使用開發(fā)者工具,創(chuàng)建一個(gè)云函數(shù),如router,然后在package.json增加tcb-router最新版latest的依賴并用npm install安裝:

  1. "dependencies": {
  2. "wx-server-sdk":"latest",
  3. "tcb-router": "latest"
  4. }

然后在index.js里輸入以下代碼,其中app.use表示該中間件適用于所有的路由,而app.router('user')則適用于路由為字符串'user'的中間件,ctx.body為返回給小程序端的數(shù)據(jù),返回的方式是通過return app.serve():

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV,
  4. })
  5. const TcbRouter = require('tcb-router');
  6. exports.main = async (event, context) => {
  7. const app = new TcbRouter({event})
  8. const {OPENID} = cloud.getWXContext()
  9. app.use(async (ctx, next) => {//適用于所有的路由
  10. ctx.data = {} //聲明data為一個(gè)對象
  11. await next();
  12. })
  13. app.router('user',async (ctx, next)=>{//路由為user
  14. ctx.data.openId = OPENID
  15. ctx.data.name = '李東bbsky'
  16. ctx.data.interest = ["爬山","旅游","讀書"]
  17. ctx.body ={ //返回到小程序端的數(shù)據(jù)
  18. "openid":ctx.data.openId,
  19. "姓名":ctx.data.name,
  20. "興趣":ctx.data.interest
  21. }
  22. })
  23. return app.serve()
  24. }

而在小程序端,我們可以用事件處理函數(shù)或者生命周期函數(shù)來調(diào)用創(chuàng)建好的router云函數(shù),就能在res對象里獲取到云函數(shù)router返回的ctx.body里的對象了:

  1. wx.cloud.callFunction({
  2. name: 'router',
  3. data: {
  4. $url: "user", //路由為字符串user,注意屬性為 $url
  5. }
  6. }).then(res => {
  7. console.log(res)
  8. })

二、tcb-router管理數(shù)據(jù)庫的增刪改查

使用tcb-router還可以管理數(shù)據(jù)庫的集合,我們可以把一個(gè)集合(也可以是多個(gè)集合)的add、remove、update、get等集成到一個(gè)云函數(shù)里,可以看下面具體的案例,我們在router云函數(shù)里輸入以下代碼:

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV,
  4. })
  5. const TcbRouter = require('tcb-router');
  6. const db = cloud.database()
  7. const _ = db.command
  8. const $ = db.command.aggregate
  9. exports.main = async (event, context) => {
  10. const collection= "" //數(shù)據(jù)庫的名稱
  11. const app = new TcbRouter({event})
  12. const {adddata,deleteid,updatedata,querydata,updateid,updatequery} = event
  13. app.use(async (ctx, next) => {
  14. ctx.data = {}
  15. await next();
  16. });
  17. app.router('add',async (ctx, next)=>{
  18. const addresult = await db.collection(collection).add({
  19. data:adddata
  20. })
  21. ctx.data.addresult = addresult
  22. ctx.body = {"添加記錄的返回結(jié)果":ctx.data.addresult}
  23. })
  24. app.router('delete',async(ctx,next)=>{
  25. const deleteresult = await db.collection(collection).where({
  26. id:deleteid
  27. }).remove()
  28. ctx.data.deleteresult = deleteresult
  29. ctx.body = {"刪除記錄的返回結(jié)果":ctx.data.deleteresult}
  30. })
  31. app.router('update',async(ctx,next)=>{
  32. const getdata = await db.collection(collection).where({
  33. id:updateid
  34. }).update({
  35. data:updatedata
  36. })
  37. ctx.data.getresult = getdata
  38. ctx.body = {"查詢記錄的返回結(jié)果":ctx.data.getresult}
  39. })
  40. app.router('get',async(ctx,next)=>{
  41. const getdata = await db.collection(collection).where(querydata).get()
  42. ctx.data.getresult = getdata
  43. ctx.body = {"查詢記錄的返回結(jié)果":ctx.data.getresult}
  44. })
  45. return app.serve();
  46. }

然后再在小程序端相應(yīng)的事件處理函數(shù)里使用wx.cloud.callFunction傳入相應(yīng)的云函數(shù)以及相應(yīng)的路由$url以及傳入對應(yīng)的data值即可:

  1. //新增一條記錄
  2. wx.cloud.callFunction({
  3. name: 'router',//router云函數(shù)
  4. data: {
  5. $url: "add",
  6. adddata:{
  7. id:"202006031020",
  8. title:"云數(shù)據(jù)庫的最佳實(shí)踐",
  9. content:"<p>文章的富文本內(nèi)容</p>",
  10. createTime:Date.now()
  11. }
  12. }
  13. }).then(res => {
  14. console.log(res)
  15. })
  16. //刪除一條記錄
  17. wx.cloud.callFunction({
  18. name: 'router',
  19. data: {
  20. $url:"delete",
  21. deleteid:"202006031020"
  22. }
  23. }).then(res => {
  24. console.log(res)
  25. })
  26. //查詢記錄
  27. wx.cloud.callFunction({
  28. name: 'router',
  29. data: {
  30. $url:"get",
  31. querydata:{
  32. id:"202006031020",
  33. }
  34. }
  35. }).then(res => {
  36. console.log(res)
  37. })

關(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ù)里,對并發(fā)有比較高要求的云函數(shù)建議不要把用tcb-router整到一個(gè)里面。每個(gè)云函數(shù)的并發(fā)數(shù)上限為1000,這本可以每秒處理十萬級別的請求,但是如果把大量不同的云函數(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)的可能性了;
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號