API › @builder.io/qwik-city/middleware/request-handler
AbortMessage
export declare class AbortMessage
append
使用 Set-Cookie
头部追加 Response
cookie 头部。
set()
和 append()
之间的区别在于,如果指定的头部已经存在,set()
将用新值覆盖现有值,而 append()
将将新值追加到值集的末尾。
append(name: string, value: string | number | Record<string, any>, options?: CookieOptions): void;
参数
类型
描述
name
string
value
string | number | Record<string, any>
options
(可选)
void
CacheControl
export type CacheControl =
| CacheControlOptions
| number
| "day"
| "week"
| "month"
| "year"
| "no-cache"
| "immutable"
| "private";
ClientConn
export interface ClientConn
属性
修饰符
类型
描述
string
(可选)
string
(可选)
Cookie
export interface Cookie
方法
描述
使用 Set-Cookie
头部追加 Response
cookie 头部。
set()
和 append()
之间的区别在于,如果指定的头部已经存在,set()
将用新值覆盖现有值,而 append()
将将新值追加到值集的末尾。
使用 Response
cookie 头部按名称删除 cookie 值。
按名称获取 Request
cookie 头部值。
获取所有 Request
cookie 头部。
检查 Request
cookie 头部名称是否存在。
返回所有设置的 Response
Set-Cookie
头部值的数组。
使用 Set-Cookie
头部设置 Response
cookie 头部。
CookieOptions
https://mdn.org.cn/en-US/docs/Web/HTTP/Headers/Set-Cookie
export interface CookieOptions
属性
修饰符
类型
描述
string
(可选) 定义将发送 cookie 的主机。如果省略,此属性默认为当前文档 URL 的主机,不包括子域。
Date | string
(可选) 以 HTTP 日期时间戳的形式指示 cookie 的最大生命周期。如果同时设置了 expires
和 maxAge
,则 maxAge
优先。
boolean
(可选) 禁止 JavaScript 访问 cookie,例如,通过 document.cookie
属性。
number | [number, 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks']
(可选) 指示 cookie 过期前的秒数。零或负数将立即使 cookie 过期。如果同时设置了 expires
和 maxAge
,则 maxAge
优先。您还可以使用数组语法以分钟、小时、天或周为单位设置最大年龄。例如,{ maxAge: [3, "days"] }
将使 cookie 在 3 天后过期。
string
(可选) 指示请求 URL 中必须存在的路径,以便浏览器发送 Cookie 头部。
'strict' | 'lax' | 'none' | 'Strict' | 'Lax' | 'None' | boolean
(可选) 控制 cookie 是否随跨站点请求发送,提供一定程度的防止跨站点请求伪造攻击 (CSRF) 的保护。
boolean
(可选) 指示只有在使用 https:
方案(除 localhost 外)进行请求时,才会将 cookie 发送到服务器。
CookieValue
export interface CookieValue
属性
修饰符
类型
描述
<T = unknown>() => T
() => number
string
data
data: T;
DeferReturn
export type DeferReturn<T> = () => Promise<T>;
delete
使用 Response
cookie 头部按名称删除 cookie 值。
delete(name: string, options?: Pick<CookieOptions, 'path' | 'domain' | 'sameSite'>): void;
参数
类型
描述
name
string
options
Pick<CookieOptions, 'path' | 'domain' | 'sameSite'>
(可选)
void
EnvGetter
export interface EnvGetter
方法
描述
get
按名称获取 Request
cookie 头部值。
get(name: string): CookieValue | null;
参数
类型
描述
name
string
CookieValue | null
getAll
获取所有 Request
cookie 头部。
getAll(): Record<string, CookieValue>;
返回
Record<string, CookieValue>
getErrorHtml
export declare function getErrorHtml(status: number, e: any): string;
参数
类型
描述
status
number
e
any
string
has
检查 Request
cookie 头部名称是否存在。
has(name: string): boolean;
参数
类型
描述
name
string
boolean
headers
返回所有设置的 Response
Set-Cookie
头部值的数组。
headers(): string[];
返回
string[]
mergeHeadersCookies
mergeHeadersCookies: (headers: Headers, cookies: CookieInterface) => Headers;
参数
类型
描述
headers
Headers
cookies
Headers
RedirectMessage
export declare class RedirectMessage extends AbortMessage
扩展自: AbortMessage
RequestEvent
export interface RequestEvent<PLATFORM = QwikCityPlatform> extends RequestEventCommon<PLATFORM>
扩展自: RequestEventCommon<PLATFORM>
属性
修饰符
类型
描述
readonly
boolean
如果中间件链已完成执行,则为 true。
readonly
() => WritableStream<Uint8Array>
用于写入 HTTP 响应流的低级访问。一旦调用 getWritableStream()
,状态和头部将不再可修改,并将通过网络发送。
readonly
boolean
如果头部已发送,则为 true,阻止设置更多头部。
readonly
() => Promise<void>
调用链中的下一个中间件函数。
注意:确保对 next()
的调用被 await
。
RequestEventAction
export interface RequestEventAction<PLATFORM = QwikCityPlatform> extends RequestEventCommon<PLATFORM>
扩展自: RequestEventCommon<PLATFORM>
属性
修饰符
类型
描述
<T extends Record<string, any>>(status: number, returnData: T) => FailReturn<T>
RequestEventBase
export interface RequestEventBase<PLATFORM = QwikCityPlatform>
属性
修饰符
类型
描述
readonly
string
请求的基路径名,可以在构建时配置。默认为 /
。
readonly
(cacheControl: CacheControl, target?: CacheControlTarget) => void
用于设置 Cache-Control 头部的便捷方法。根据您的 CDN,您可能需要添加另一个 cacheControl,并将第二个参数设置为 CDN-Cache-Control
或任何其他值(我们提供了最常见的自动完成值,但您可以使用任何您想要的字符串)。
有关更多信息,请参阅 https://mdn.org.cn/en-US/docs/Web/HTTP/Headers/Cache-Control 和 https://qwik.node.org.cn/docs/caching/\#CDN-Cache-Controls。
readonly
提供有关客户端连接的信息,例如 IP 地址和请求来源的国家/地区。
readonly
HTTP 请求和响应 cookie。使用 get()
方法检索请求 cookie 值。使用 set()
方法设置响应 cookie 值。
https://mdn.org.cn/en-US/docs/Web/HTTP/Cookies
readonly
平台提供的环境变量。
readonly
Headers
HTTP 响应头部。请注意,它在您第一次添加头部之前将为空。如果您想读取请求头部,请使用 request.headers
代替。
https://mdn.org.cn/en-US/docs/Glossary/Response\_header
readonly
string
HTTP 请求方法。
https://mdn.org.cn/en-US/docs/Web/HTTP/Methods
readonly
Readonly<Record<string, string>>
从当前 URL 路径名段解析的 URL 路径参数。使用 query
代替检索查询字符串搜索参数。
readonly
() => Promise<unknown>
此方法将检查请求头部以获取 Content-Type
头部,并相应地解析主体。它支持 application/json
、application/x-www-form-urlencoded
和 multipart/form-data
内容类型。
如果未设置 Content-Type
头部,它将返回 null
。
readonly
string
URL 路径名。不包括协议、域、查询字符串(搜索参数)或哈希值。
https://mdn.org.cn/en-US/docs/Web/API/URL/pathname
readonly
PLATFORM
平台特定数据和函数
readonly
URLSearchParams
URL 查询字符串 (URL 搜索参数)。使用 params
代替检索 URL 路径名中的路由参数。
https://mdn.org.cn/en-US/docs/Web/API/URLSearchParams
readonly
Request
HTTP 请求信息。
readonly
Map<string, any>
所有请求处理程序共享的映射。每个 HTTP 请求都将获得一个新的共享映射实例。共享映射对于在请求处理程序之间共享数据很有用。
readonly
AbortSignal
请求的 AbortSignal(与 request.signal
相同)。此信号表示请求已被中止。
readonly
URL
HTTP 请求 URL。
RequestEventCommon
export interface RequestEventCommon<PLATFORM = QwikCityPlatform> extends RequestEventBase<PLATFORM>
扩展: RequestEventBase<PLATFORM>
属性
修饰符
类型
描述
readonly
(statusCode: ErrorCodes, message: string) => ErrorResponse
调用时,响应将立即以给定的状态码结束。这对于以 404
结束响应并使用路由目录中的 404 处理程序可能很有用。有关应使用哪个状态码,请参阅 https://mdn.org.cn/en-US/docs/Web/HTTP/Status。
readonly
() => AbortMessage
readonly
(statusCode: StatusCodes, html: string) => AbortMessage
用于发送 HTML 主体响应的便捷方法。响应将自动将 Content-Type
标头设置为 text/html; charset=utf-8
。html()
响应只能调用一次。
readonly
(statusCode: StatusCodes, data: any) => AbortMessage
用于将数据 JSON 字符串化并在响应中发送的便捷方法。响应将自动将 Content-Type
标头设置为 application/json; charset=utf-8
。json()
响应只能调用一次。
readonly
(local?: string) => string
内容所在的语言环境。
语言环境值可以使用 getLocale()
从选定的方法中检索。
readonly
(statusCode: RedirectCode, url: string) => RedirectMessage
要重定向到的 URL。调用时,响应将立即以正确的重定向状态和标头结束。
https://mdn.org.cn/en-US/docs/Web/HTTP/Redirections
readonly
SendMethod
发送主体响应。使用 send()
时,Content-Type
响应标头不会自动设置,必须手动设置。send()
响应只能调用一次。
readonly
(statusCode?: StatusCodes) => number
HTTP 响应状态码。在使用参数调用时设置状态码。始终返回状态码,因此在不使用参数的情况下调用 status()
可以用于返回当前状态码。
https://mdn.org.cn/en-US/docs/Web/HTTP/Status
readonly
(statusCode: StatusCodes, text: string) => AbortMessage
用于发送文本主体响应的便捷方法。响应将自动将 Content-Type
标头设置为 text/plain; charset=utf-8
。text()
响应只能调用一次。
RequestEventLoader
export interface RequestEventLoader<PLATFORM = QwikCityPlatform> extends RequestEventAction<PLATFORM>
扩展: RequestEventAction<PLATFORM>
属性
修饰符
类型
描述
<T>(returnData: Promise<T> | (() => Promise<T>)) => DeferReturn<T>
requestHandler
export type RequestHandler<PLATFORM = QwikCityPlatform> = (
ev: RequestEvent<PLATFORM>,
) => Promise<void> | void;
引用: RequestEvent
RequestHandler
export type RequestHandler<PLATFORM = QwikCityPlatform> = (
ev: RequestEvent<PLATFORM>,
) => Promise<void> | void;
引用: RequestEvent
ResolveSyncValue
export interface ResolveSyncValue
ResolveValue
export interface ResolveValue
ServerError
export declare class ServerError<T = Record<any, any>> extends Error
扩展: Error
构造函数
修饰符
描述
构造 ServerError
类的新实例
属性
修饰符
类型
描述
T
number
ServerRenderOptions
export interface ServerRenderOptions extends RenderOptions
扩展: RenderOptions
属性
修饰符
类型
描述
boolean
(可选) 防范跨站点请求伪造 (CSRF) 攻击的保护措施。
当 true
时,对于每个传入的 POST、PUT、PATCH 或 DELETE 表单提交,都会检查请求来源是否与服务器来源匹配。
在禁用此选项时要小心,因为它可能会导致 CSRF 攻击。
默认为 true
。
QwikCityPlan
Render
ServerRequestEvent
由服务器创建的请求事件。
export interface ServerRequestEvent<T = unknown>
属性
修饰符
类型
描述
() => ClientConn
string | undefined
QwikCityPlatform
Request
URL
ServerRequestMode
export type ServerRequestMode = "dev" | "static" | "server";
ServerResponseHandler
export type ServerResponseHandler<T = any> = (
status: number,
headers: Headers,
cookies: Cookie,
resolve: (response: T) => void,
requestEv: RequestEventInternal,
) => WritableStream<Uint8Array>;
引用: Cookie
set
使用 Set-Cookie
头部设置 Response
cookie 头部。
set(name: string, value: string | number | Record<string, any>, options?: CookieOptions): void;
参数
类型
描述
name
string
value
string | number | Record<string, any>
options
(可选)
void
status
status: number;