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

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的盐池网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
tcb-router主要用于小程序端调用云函数时的处理逻辑,在小程序端使用wx.cloud.callFunction调用云函数时,我们需要在name里传入要调用的云函数名称,以及在data里传入要调用的路由的路径;而在云函数端使用app.router来写对应的路由的处理函数。
使用开发者工具,创建一个云函数,如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为返回给小程序端的数据,返回的方式是通过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()
}
而在小程序端,我们可以用事件处理函数或者生命周期函数来调用创建好的router云函数,就能在res对象里获取到云函数router返回的ctx.body里的对象了:
wx.cloud.callFunction({
  name: 'router',
  data: {
    $url: "user", //路由为字符串user,注意属性为 $url 
  }
}).then(res => {
    console.log(res)
})
使用tcb-router还可以管理数据库的集合,我们可以把一个集合(也可以是多个集合)的add、remove、update、get等集成到一个云函数里,可以看下面具体的案例,我们在router云函数里输入以下代码:
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();
}
然后再在小程序端相应的事件处理函数里使用wx.cloud.callFunction传入相应的云函数以及相应的路由$url以及传入对应的data值即可:
//新增一条记录
wx.cloud.callFunction({
  name: 'router',//router云函数
  data: {
  $url: "add",
  adddata:{
    id:"202006031020",
    title:"云数据库的最佳实践",
    content:"文章的富文本内容
",
    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)
})
关于tcb-router更多进阶用法,可以查看技术文档:tcb-router Github地址。使用tcb-router时的一些说明:
                分享题目:创新互联小程序云教程:云开发云函数路由tcb-router
                
                本文地址:http://www.csdahua.cn/qtweb/news23/262973.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网