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://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
export interface CookieOptions
屬性
修飾符
類型
說明
string
(可選) 定義要將 Cookie 發送到哪個主機。如果省略,則此屬性預設為當前文檔 URL 的主機,不包括子網域。
Date | string
(可選) 表示 Cookie 的最大生存期,以 HTTP 日期時間戳記表示。如果同時設置了 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:
方案發出請求時,才會將 Cookie 發送到伺服器(localhost 除外)
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
設定快取控制標頭的便捷方法。根據您的 CDN,您可能需要新增另一個 cacheControl,並將第二個參數設定為 CDN-Cache-Control
或任何其他值(我們提供最常見的值以供自動完成,但您可以使用任何您想要的字串)。
如需更多資訊,請參閱 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control 和 https://qwik.dev.org.tw/docs/caching/\#CDN-Cache-Controls。
readonly
提供有關客戶端連線的資訊,例如 IP 地址和請求來源國家/地區。
readonly
HTTP 請求和回應 Cookie。使用 get()
方法擷取請求 Cookie 值。使用 set()
方法設定回應 Cookie 值。
https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies
readonly
平台提供的環境變數。
readonly
Headers
HTTP 回應標頭。請注意,在您第一次添加標頭之前,它將是空的。如果要讀取請求標頭,請改用 request.headers
。
https://developer.mozilla.org/en-US/docs/Glossary/Response\_header
readonly
string
HTTP 請求方法。
https://developer.mozilla.org/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://developer.mozilla.org/en-US/docs/Web/API/URL/pathname
readonly
PLATFORM
平台特定數據和函數
readonly
URLSearchParams
URL 查詢字串(URL 搜尋參數)。使用 params
擷取在 URL 路徑名稱中找到的路由參數。
https://developer.mozilla.org/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://developer.mozilla.org/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
重新導向的網址。呼叫時,回應將立即以正確的重新導向狀態和標頭結束。
https://developer.mozilla.org/zh-TW/docs/Web/HTTP/Redirections
readonly
SendMethod
傳送主體回應。使用 send()
時,不會自動設定 Content-Type
回應標頭,必須手動設定。send()
回應只能呼叫一次。
readonly
(statusCode?: StatusCodes) => number
HTTP 回應狀態碼。使用參數呼叫時設定狀態碼。始終返回狀態碼,因此呼叫沒有參數的 status()
可用於返回目前的狀態碼。
https://developer.mozilla.org/zh-TW/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>
請求處理常式
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
伺服器錯誤
export declare class ServerError<T = Record<any, any>> extends Error
繼承: 錯誤
建構函式
修飾符
說明
建構 ServerError
類別的新執行個體
屬性
修飾符
類型
說明
T
number
伺服器渲染選項
export interface ServerRenderOptions extends RenderOptions
繼承: RenderOptions
屬性
修飾符
類型
說明
boolean
(可選) 防止跨站請求偽造 (CSRF) 攻擊的保護。
當 true
時,對於每個傳入的 POST、PUT、PATCH 或 DELETE 表單提交,會檢查請求來源是否與伺服器的來源相符。
停用此選項時請小心,因為這可能會導致 CSRF 攻擊。
預設值為 true
。
QwikCityPlan
Render
伺服器請求事件
由伺服器建立的請求事件。
export interface ServerRequestEvent<T = unknown>
屬性
修飾符
類型
說明
() => ClientConn
字串 | 未定義
QwikCityPlatform
Request
URL
伺服器請求模式
export type ServerRequestMode = "dev" | "static" | "server";
伺服器回應處理常式
export type ServerResponseHandler<T = any> = (
status: number,
headers: Headers,
cookies: Cookie,
resolve: (response: T) => void,
requestEv: RequestEventInternal,
) => WritableStream<Uint8Array>;
參考: Cookie
設定
使用 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: number;