Loading...
Loading...
Compare original and translation side by side
exports.handlerhandlerimport type { Context, Config } from "@netlify/functions";
export default async (req: Request, context: Context) => {
return new Response("Hello, world!");
};
export const config: Config = {
path: "/api/hello",
};RequestResponseContextexports.handlerhandlerimport type { Context, Config } from "@netlify/functions";
export default async (req: Request, context: Context) => {
return new Response("Hello, world!");
};
export const config: Config = {
path: "/api/hello",
};RequestResponseContextnetlify/functions/netlify/functions/
_shared/ # Non-function shared code (underscore prefix)
auth.ts
db.ts
items.ts # -> /.netlify/functions/items (or custom path via config)
users/index.ts # -> /.netlify/functions/users.ts.mts.ts.js.jsnetlify/functions/netlify/functions/
_shared/ # 非函数类共享代码(下划线前缀)
auth.ts
db.ts
items.ts # -> /.netlify/functions/items(或通过config配置自定义路径)
users/index.ts # -> /.netlify/functions/users.ts.mts.ts.js.jsconfigexport const config: Config = {
path: "/api/items", // Static path
// path: "/api/items/:id", // Path parameter
// path: ["/api/items", "/api/items/:id"], // Multiple paths
// excludedPath: "/api/items/special", // Excluded paths
// preferStatic: true, // Don't override static files
};path/.netlify/functions/{name}pathcontext.params// config: { path: "/api/items/:id" }
export default async (req: Request, context: Context) => {
const { id } = context.params;
// ...
};configexport const config: Config = {
path: "/api/items", // 静态路径
// path: "/api/items/:id", // 路径参数
// path: ["/api/items", "/api/items/:id"], // 多路径配置
// excludedPath: "/api/items/special", // 排除路径
// preferStatic: true, // 不覆盖静态文件
};path/.netlify/functions/{函数名}pathcontext.params// config: { path: "/api/items/:id" }
export default async (req: Request, context: Context) => {
const { id } = context.params;
// ...
};export default async (req: Request, context: Context) => {
switch (req.method) {
case "GET": return handleGet(context.params.id);
case "POST": return handlePost(await req.json());
case "DELETE": return handleDelete(context.params.id);
default: return new Response("Method not allowed", { status: 405 });
}
};
export const config: Config = {
path: "/api/items/:id",
method: ["GET", "POST", "DELETE"],
};export default async (req: Request, context: Context) => {
switch (req.method) {
case "GET": return handleGet(context.params.id);
case "POST": return handlePost(await req.json());
case "DELETE": return handleDelete(context.params.id);
default: return new Response("Method not allowed", { status: 405 });
}
};
export const config: Config = {
path: "/api/items/:id",
method: ["GET", "POST", "DELETE"],
};202-backgroundnetlify/functions/process-background.ts202-backgroundnetlify/functions/process-background.tsexport default async (req: Request) => {
const { next_run } = await req.json();
console.log("Next invocation at:", next_run);
};
export const config: Config = {
schedule: "@hourly", // or cron: "0 * * * *"
};@yearly@monthly@weekly@daily@hourlyexport default async (req: Request) => {
const { next_run } = await req.json();
console.log("Next invocation at:", next_run);
};
export const config: Config = {
schedule: "@hourly", // 或cron表达式: "0 * * * *"
};@yearly@monthly@weekly@daily@hourlyReadableStreamexport default async (req: Request) => {
const stream = new ReadableStream({ /* ... */ });
return new Response(stream, {
headers: { "Content-Type": "text/event-stream" },
});
};ReadableStreamexport default async (req: Request) => {
const stream = new ReadableStream({ /* ... */ });
return new Response(stream, {
headers: { "Content-Type": "text/event-stream" },
});
};| Property | Description |
|---|---|
| Path parameters from config |
| |
| Client IP address |
| |
| |
| |
| Team account ID |
| Unique request ID |
| Extend execution after response is sent |
| 属性 | 说明 |
|---|---|
| 来自配置的路径参数 |
| 地理位置信息: |
| 客户端IP地址 |
| 提供 |
| 部署信息: |
| 站点信息: |
| 团队账户ID |
| 唯一请求ID |
| 发送响应后延长函数执行时间 |
Netlify.envprocess.envconst apiKey = Netlify.env.get("API_KEY");Netlify.envprocess.envconst apiKey = Netlify.env.get("API_KEY");| Resource | Limit |
|---|---|
| Synchronous timeout | 60 seconds |
| Background timeout | 15 minutes |
| Scheduled timeout | 30 seconds |
| Memory | 1024 MB |
| Buffered payload | 6 MB |
| Streamed payload | 20 MB |
| 资源 | 限制 |
|---|---|
| 同步函数超时时间 | 60秒 |
| 后台函数超时时间 | 15分钟 |
| 定时函数超时时间 | 30秒 |
| 内存 | 1024 MB |
| 缓冲负载 | 6 MB |
| 流式负载 | 20 MB |