1、下载tcb-router包
cnpm i tcb-router -D
2、引入tcb-router包
const TcbRouter = require('tcb-router');
3、初始化router包
在main内部执行(cloud初始化后)
const app = new TcbRouter({
event
});
4、使用路由
app.use(async(ctx, next) => {
ctx.body = {};
console.log(ctx)
await next()
})
// 设置number
app.router(["addNumber",'cutNumber'],async (ctx,next)=>{
let number = ctx._req.url == "addNumber"?1:-1
try{
let data = await db.collection('num').doc("781218b5-c0a7-4453-befe-a02b58b6a921").update({
data:{
num: db.command.inc(number)
}
})
ctx.body = { data: "", code: 1, msg: "操作成功" }
}catch(e){
ctx.body = { data: "", code: 0, msg: "操作失败,"+e }
}
})
// 获取number
app.router('getData', async(ctx, next) => {
try {
let data = await db.collection('num').get();
ctx.body = {
data: data.data,
code: 1
}
await next()
} catch (e) {
ctx.body = {
data: "",
code: 0,
msg: e
}
await next()
}
})
return app.serve();
网友评论