跳转到内容
搜索文档

API 参考

最后更新 查看 MarkdownAgent 设置

以下方法可用于配置 Pages Function。

方法

onRequests

除非导出更具体的 onRequestVerb 方法,否则将调用 onRequest 方法。例如,若同时导出 onRequestonRequestGet,则 GET 请求只会调用 onRequestGet

  • onRequest(contextEventContext) Response | Promise<Response>

    • 只要未导出特定请求动词(如下方方法),此函数会在所有请求上被调用,无论请求方法为何。
  • onRequestGet(contextEventContext) Response | Promise<Response>

    • 此函数会在所有 GET 请求上被调用。
  • onRequestPost(contextEventContext) Response | Promise<Response>

    • 此函数会在所有 POST 请求上被调用。
  • onRequestPatch(contextEventContext) Response | Promise<Response>

    • 此函数会在所有 PATCH 请求上被调用。
  • onRequestPut(contextEventContext) Response | Promise<Response>

    • 此函数会在所有 PUT 请求上被调用。
  • onRequestDelete(contextEventContext) Response | Promise<Response>

    • 此函数会在所有 DELETE 请求上被调用。
  • onRequestHead(contextEventContext) Response | Promise<Response>

    • 此函数会在所有 HEAD 请求上被调用。
  • onRequestOptions(contextEventContext) Response | Promise<Response>

    • 此函数会在所有 OPTIONS 请求上被调用。

env.ASSETS.fetch()

env.ASSETS.fetch() 函数允许你从 Pages 项目获取静态资源。

可向 env.ASSETS.fetch() 函数传递 Request 对象、URL 字符串或 URL 对象。URL 必须是规范路径,而非直接指向资源。例如,若路径为 /users/index.html,应请求 /users/ 而非 /users/index.html。此调用会运行 header 和重定向规则,修改返回的响应。

类型

EventContext

以下属性位于传递给 onRequest 方法的 context 对象上:

  • request Request

    这是传入的 Request

  • functionPath string

    这是请求的路径。

  • waitUntil(promisePromise<any>) void

    更多信息请参阅 waitUntil 文档

  • passThroughOnException() void

    更多信息请参阅 passThroughOnException 文档。请注意,这在 高级模式项目 上不起作用。

  • next(input?Request | string, init?RequestInit) Promise<Response>

    将请求传递给下一个 Function,若无其他 Function 可用则传递给资源服务器。

  • env EnvWithFetch

  • params Params<P>

    保存 动态路由 的值。

    以下示例中,动态路径为 /users/[user].js。访问 /users/nevi 时,params 对象如下:

    {
    	user: "nevi";
    }

    这允许你从路径获取动态值:

    export function onRequest(context) {
    	return new Response(`Hello ${context.params.user}`);
    }

    将返回 "Hello nevi"

  • data Data

EnvWithFetch

保存 Function 的环境变量、密钥和绑定。还包含 ASSETS 绑定,用于回退到资源提供行为。

这篇文档对您有帮助吗?