以下方法可用于配置 Pages Function。
除非导出更具体的 onRequestVerb 方法,否则将调用 onRequest 方法。例如,若同时导出 onRequest 和 onRequestGet,则 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() 函数允许你从 Pages 项目获取静态资源。
可向 env.ASSETS.fetch() 函数传递 Request 对象、URL 字符串或 URL 对象。URL 必须是规范路径,而非直接指向资源。例如,若路径为 /users/index.html,应请求 /users/ 而非 /users/index.html。此调用会运行 header 和重定向规则,修改返回的响应。
以下属性位于传递给 onRequest 方法的 context 对象上:
-
requestRequest这是传入的 Request。
-
functionPathstring这是请求的路径。
-
waitUntil(promisePromise<any>)void更多信息请参阅
waitUntil文档。 -
passThroughOnException()void更多信息请参阅
passThroughOnException文档。请注意,这在 高级模式项目 上不起作用。 -
next(input?Request | string, init?RequestInit)Promise<Response>将请求传递给下一个 Function,若无其他 Function 可用则传递给资源服务器。
-
envEnvWithFetch -
paramsParams<P>保存 动态路由 的值。
以下示例中,动态路径为
/users/[user].js。访问/users/nevi时,params对象如下:{ user: "nevi"; }这允许你从路径获取动态值:
export function onRequest(context) { return new Response(`Hello ${context.params.user}`); }将返回
"Hello nevi"。 -
dataData
保存 Function 的环境变量、密钥和绑定。还包含 ASSETS 绑定,用于回退到资源提供行为。