API 文档

数据库文档捉虫大赛邀您参加,好礼多多> HOT

准备工作

  1. 安装 CLI 工具
  2. 登录 CLI 工具

初始化目录

mkdir my-cloudbase-service && cd my-cloudbase-service
mkdir functions && mkdir functions/hello
touch 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.js
exports.main = async function () {
  return "Hello World!";
};

发布云函数

执行以下命令:

cloudbase functions:deploy hello

等待之后,云函数便发布成功:

cloudbase functions:deploy hello
? 未找到函数发布配置,使用默认配置? Yes
✔ [hello] 函数部署成功!

为云函数创建 HTTP 路由

执行以下命令创建一条HTTP 路由,路径为 /hello,指向的云函数为 hello

cloudbase service:create -p /hello -f hello

通过 HTTP 访问云函数

随后便可以通过 https://${env}.ap-shanghai.app.tcloudbase.com/hello 直接访问函数,其中 ${env} 是环境 ID,ap-shanghai 为地域,根据环境所在地域不同填写对应地域。

curl https://${env}.ap-shanghai.app.tcloudbase.com/hello
hello world!

也可以直接在浏览器内打开 https://${env}.ap-shanghai.app.tcloudbase.com/hello

目录