光环标志
光环基地文档
文档›Edge Functions›Your first function

您的第一个边缘函数

您将编写一个函数,将其部署到您的项目中,从 Node.js 调用它并读取其结果。下面的每个命令都是真实运行的。

10分钟 读取·级别 初学者·修改于 2026年10月12日
该英文文本是根据法文原文自动生成的,尚未经过审查。
该页面已自动翻译。英文版具有权威性。
#
要求

开始之前

您需要:

  • Node.js 20 或更高版本。
  • aura CLI。使用 npm install -g @aurabase/cli 安装它(有关其他平台,请参阅 CLI 参考)。
  • 一个 Aurabase 帐户。
CLI 今天讲法语

aura CLI 以法语打印其消息。下面的输出是从实际运行中复制的,未翻译。

#
项目

1. 创建并链接项目

登录,创建一个项目,并将您的工作文件夹链接到它。将 <org-slug> 替换为 aura orgs list显示的段。

terminalbash
aura login --email you@example.com --password '<your-password>'
aura orgs list
aura projects create my-app --org <org-slug>

最后一个命令打印项目 URL 和您的第一个 API 密钥。 立即复制它们:初始键仅显示一次。 如果您错过了它们,请在 Studio 中的“设置”、API 密钥下创建新的。

outputtext
  ✔ Projet créé : my-app (e99f2c4f-43e2-4d08-b023-b4f9e687d17c)
    import { createClient } from '@aurabase/aurabase-js'
    const client = createClient('http://my-app-ajap5iw2.localhost:8080', 'aura_anon_…')

将当前文件夹链接到项目:

terminalbash
aura link --project-id e99f2c4f-43e2-4d08-b023-b4f9e687d17c
outputtext
  ✔ Projet e99f2c4f-… lié dans aurabase.toml.
  • URL du projet : http://my-app-ajap5iw2.localhost:8080

然后告诉 CLI 数据平面在哪里以及使用哪个密钥。部署需要 service_role 密钥,因为部署代码是一项管理操作。

.envbash
AURA_PROJECT_URL=http://my-app-ajap5iw2.localhost:8080
AURA_API_KEY=<your service_role key>
将 service_role 键保留在您的计算机上

service_role 密钥绕过行级安全性并可以部署代码。切勿将其放入浏览器捆绑包中或提交 .env。

#
代码

2. 编写函数

terminalbash
aura functions new hello
outputtext
  ✔ Nouvelle fonction créée dans aurabase/functions/hello (gabarit TypeScript Deno)

打开aurabase/functions/hello/index.ts并替换其内容:

aurabase/functions/hello/index.tstypescript
Deno.serve(async (req: Request) => {
  const { name = 'world' } = await req.json().catch(() => ({}))

  return new Response(JSON.stringify({ greeting: `Hello, ${name}!` }), {
    headers: { 'content-type': 'application/json' },
  })
})

函数必须调用 Deno.serve。 export default 永远不会被调用。

#
部署

3. 部署

terminalbash
aura functions deploy hello
outputtext
  ✔ Détection script TypeScript (runtime: deno)...
  • Envoi vers le service (runtime: deno)...
✔ Fonction 'hello' déployée avec succès !

从 CLI 调用一次以检查它是否响应:

terminalbash
aura functions invoke hello --data '{"name":"Ada"}'
outputtext
  ✔ Réponse (200) :
{
  "data": {
    "body": { "greeting": "Hello, Ada!" },
    "duration_ms": 74,
    …
#
软件开发工具包

4.从SDK中调用

terminalbash
mkdir hello-client && cd hello-client
npm init -y
npm install @aurabase/aurabase-js
hello.mjstypescript
import { createClient } from '@aurabase/aurabase-js'

const aura = createClient(process.env.AURA_PROJECT_URL, process.env.AURA_ANON_KEY)

const { data, error } = await aura.functions.invoke('hello', { body: { name: 'Ada' } })
if (error) throw error

console.log(data.greeting)
terminalbash
AURA_PROJECT_URL=http://my-app-ajap5iw2.localhost:8080 AURA_ANON_KEY=aura_anon_… node hello.mjs
outputtext
Hello, Ada!

您使用公共 anon 键调用了一个函数。这是公钥可以进行的唯一调用:查看 谁可以调用什么。

最后更新· 2026年10月12日