云开发 云函数路由tcb-router

2020-07-21 17:55 更新

tcb-router是基于Nodejs koa风格的云开发云函数轻量级的类路由库,可以用于优化前端(小程序端)调用服务端的云函数时的处理逻辑。我们可以使用它在一个云函数里集成多个类似功能的云函数,比如针对某个集合的增删改查;也可以把后端的一些零散功能集成到一个云函数里,便于集中管理等。

一、tcb-router快速入门

tcb-router主要用于小程序端调用云函数时的处理逻辑,在小程序端使用wx.cloud.callFunction调用云函数时,我们需要在name里传入要调用的云函数名称,以及在data里传入要调用的路由的路径;而在云函数端使用app.router来写对应的路由的处理函数。

使用开发者工具,创建一个云函数,如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为返回给小程序端的数据,返回的方式是通过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为一个对象
  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 ={ //返回到小程序端的数据
  18. "openid":ctx.data.openId,
  19. "姓名":ctx.data.name,
  20. "兴趣":ctx.data.interest
  21. }
  22. })
  23. return app.serve()
  24. }

而在小程序端,我们可以用事件处理函数或者生命周期函数来调用创建好的router云函数,就能在res对象里获取到云函数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管理数据库的增删改查

使用tcb-router还可以管理数据库的集合,我们可以把一个集合(也可以是多个集合)的add、remove、update、get等集成到一个云函数里,可以看下面具体的案例,我们在router云函数里输入以下代码:

  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= "" //数据库的名称
  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 = {"添加记录的返回结果":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 = {"删除记录的返回结果":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 = {"查询记录的返回结果":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 = {"查询记录的返回结果":ctx.data.getresult}
  44. })
  45. return app.serve();
  46. }

然后再在小程序端相应的事件处理函数里使用wx.cloud.callFunction传入相应的云函数以及相应的路由$url以及传入对应的data值即可:

  1. //新增一条记录
  2. wx.cloud.callFunction({
  3. name: 'router',//router云函数
  4. data: {
  5. $url: "add",
  6. adddata:{
  7. id:"202006031020",
  8. title:"云数据库的最佳实践",
  9. content:"<p>文章的富文本内容</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. })

关于tcb-router更多进阶用法,可以查看技术文档:tcb-router Github地址。使用tcb-router时的一些说明:

  • 通常情况下,我们不建议大家使用一个云函数来调用其他云函数这种做法,这种做法会导致云函数的执行时间会增加很多,而且还会耗费云函数的资源,我们可以使用tcb-router来处理需要跨云函数调用的情况;

  • 值得注意的是,tcb-router会把所有云函数的承载放在一个云函数里,对并发有比较高要求的云函数建议不要把用tcb-router整到一个里面。每个云函数的并发数上限为1000,这本可以每秒处理十万级别的请求,但是如果把大量不同的云函数都集成到一个里面,尤其是一些耗时比较长的云函数会严重拖性能后退,而这些云函数都会共享这1000个并发,所以要注意根据情况来抉择了;

  • 云函数会有一个冷启动时间(比如十分钟以上没人调用这个云函数,当再首次调用这个云函数会比较慢),当我们把多个功能相似、并发不会特别高(低于每秒几千)的云函数使用tcb-router集成到一个云函数里,这样就可以减少冷启动的可能性了;
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号