- tcb-router-based koa-style small program to develop cloud-cloud routing function lightweight class library, mainly used to optimize server-side processing logic function
- It can be divided into many tcb-router based on routing function to handle a cloud service environment.
- A user can only create 50 cloud function in a cloud environment
- Similar request is classified into a same function processing
tcb-router
Koa equivalent middleware
equivalent model of an onion, each layer corresponds to a middleware, the implementation of one level, only been performed after the last, before returning
REQUEST request
response in response to
installation
Cloud function entry file
cnpm install tcb-router
Function introduced in your cloud files
const TcbRouter = require('tcb-router')
Instructions
exports.main = async (event, context) => {
const app = new TcbRouter({event})
// 播放歌曲
app.router('musicUrl',async(ctx,next) =>{
ctx.body = await rp(BASE_URL+ `/song/url?id=${event.musicId}`).then((res) =>{
return res
})
})
// 歌词
app.router('lyric', async (ctx, next) => {
ctx.body = await rp(BASE_URL + `/lyric?id=${event.musicId}`).then((res) => {
return res
})
})
return app.serve()
}
Quite koa write interface and we use the same interface name defined url
use
Interface calls within the file we need
which name
cloud the function name to be called, $url
to call routing path
wx.cloud.callFunction({
name: 'music',
data: {
musicId,
$url: 'lyric'
}
}).then((res) =>{
const lrc = JSON.parse(res.result).lrc
if(lrc) {
lyric = lrc.lyric
}
this.setData({
lyric
})
})
These are all of the content, the final results visit https://github.com/MrZHLF/wx-music