快速开始
#
准备工作- 安装 CLI 工具
- 登录 CLI 工具
#
初始化目录mkdir my-cloudbase-service && cd my-cloudbase-servicemkdir functions && mkdir functions/hellotouch cloudbaserc.json functions/hello/index.js
然后我们获得了一个如下结构的目录:
.├── cloudbaserc.json└── functions └── hello └── index.js
cloudbaserc.json
内,填入环境 ID:
// cloudbaserc.json{ "envId": "your-env-id"}
functions/hello/index.js
内,我们写入一个简单的 Hello World:
// functions/hello/index.jsexports.main = async function () { return "Hello World!";};
#
发布云函数执行以下命令:
cloudbase fn deploy hello
等待之后,云函数便发布成功:
cloudbase fn deploy hello? 未找到函数发布配置,使用默认配置? Yes✔ [hello] 函数部署成功!
#
为云函数创建 HTTP 路由执行以下命令创建一条HTTP 路由,路径为 /hello
,指向的云函数为 hello
:
cloudbase service create -p hello -f hello
注意:CLI 版本需为 1.0.6 以上。
#
通过 HTTP 访问云函数随后便可以通过 https://${env}.service.tcloudbase.com/hello
直接访问函数,其中 ${env}
是环境 ID
curl https://${env}.service.tcloudbase.com/hellohello world!
也可以直接在浏览器内打开 https://${env}.service.tcloudbase.com/hello