Edge Functions 入门
了解无服务器函数的概念,以及基于 Deno 构建的 Supabase Edge Functions 如何增强后端能力。
Edge Functions 入门 是 CoddyKit 上的免费 Supabase Backend as a Service 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Supabase Backend as a Service 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Supabase Backend as a Service 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Say Hello to Serverless!
Welcome to Supabase Edge Functions! Before we dive in, let's understand serverless functions. Traditionally, you'd manage servers to run your backend code.
With serverless, you write code, and a cloud provider handles all the server management for you. You just focus on your logic!
Why Go Serverless?
Serverless functions offer some powerful advantages:
- Automatic Scaling: They scale up or down instantly with demand.
- Pay-per-use: You only pay for the compute time your functions actually run.
- Reduced Ops: No servers to provision, patch, or maintain.
- Faster Development: Focus purely on writing your application logic.
Supabase's Serverless Power
Supabase offers its own brand of serverless functions called Edge Functions. They extend your Supabase project's capabilities without needing to set up a separate server.
Think of them as tiny pieces of backend logic that run on demand, close to your users.
Powered by Deno
Supabase Edge Functions are built on Deno, a secure runtime for JavaScript and TypeScript. Deno is a modern alternative to Node.js.
This means you can write your functions in TypeScript right out of the box, enjoying type safety and modern JavaScript features without complex setup.
How Edge Functions Work
Edge Functions are event-driven. They only execute when triggered by an event, like an HTTP request from your application or a webhook.
They are also stateless, meaning each execution is independent. This design helps them scale efficiently.
What Can They Do?
Edge Functions are incredibly versatile! Here are some common use cases:
- Custom API Endpoints: Build your own API routes.
- Webhook Handlers: Process events from other services.
- Data Transformations: Modify data before or after database operations.
- Backend Logic: Run complex calculations or integrate with external APIs.
A Simple 'Hello' Function
Let's look at a basic Edge Function. This Deno code defines a function that responds to an HTTP request, expecting a name in its body.
Try running it to see the simple JSON response!
Deno.serve(async (req) => {
const { name } = await req.json();
return new Response(
JSON.stringify({ message: `Hello, ${name || 'World'}!` }),
{
headers: { 'Content-Type': 'application/json' },
status: 200,
},
);
});The Power of the 'Edge'
The 'Edge' in Edge Functions means they run geographically close to your users. This significantly reduces latency, making your applications feel faster and more responsive.
Supabase handles deploying your functions to a global network of servers for optimal performance.
Invoking Your Function
Once deployed, you can easily call your Edge Functions from your client-side application using the Supabase client library. Here's a JavaScript example for our 'hello-world' function:
import { createClient } from '@supabase/supabase-js';
// Replace with your actual Supabase project URL and Anon Key
const supabaseUrl = 'https://your-project-ref.supabase.co';
const supabaseAnonKey = 'YOUR_ANON_KEY';
const supabase = createClient(supabaseUrl, supabaseAnonKey);
async function callHelloFunction() {
const { data, error } = await supabase.functions.invoke('hello-world', {
body: { name: 'CoddyKit User' },
});
if (error) {
console.error('Error invoking function:', error);
} else {
console.log('Function response:', data);
}
}
callHelloFunction();Edge Function Essentials
Let's check your understanding of Supabase Edge Functions!
Recap: Edge Functions Intro
Great job! You've learned the basics of serverless computing and Supabase Edge Functions.
- Edge Functions are Supabase's serverless offering.
- They run on the Deno runtime, supporting TypeScript.
- They offer automatic scaling, pay-per-use, and reduced ops.
- They execute on demand, close to your users.
Next, we'll learn how to deploy your first function!
常见问题解答
「Edge Functions 入门」课时是免费的吗?
是的 — 「Edge Functions 入门」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Supabase Backend as a Service 课程的其余内容,请升级到 CoddyKit PRO。 Supabase Backend as a Service 课程共包含 4 节课。
「Edge Functions 入门」这节课中我会学到什么?
了解无服务器函数的概念,以及基于 Deno 构建的 Supabase Edge Functions 如何增强后端能力。 你通过在浏览器中直接运行的动手代码来练习 Supabase Backend as a Service,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Supabase Backend as a Service 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Supabase Backend as a Service 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「Edge Functions 入门」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Supabase Backend as a Service 课中编写并运行代码吗?
能。每节 Supabase Backend as a Service 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Edge Functions 入门
- 部署您的第一个函数
- 将函数集成到应用中
- 密钥、环境变量与定时函数