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

CookieOptions

(可選)

void

CacheControl

export type CacheControl =
  | CacheControlOptions
  | number
  | "day"
  | "week"
  | "month"
  | "year"
  | "no-cache"
  | "immutable"
  | "private";

編輯此區塊

ClientConn

export interface ClientConn

屬性

修飾符

類型

說明

country?

string

(可選)

ip?

string

(可選)

編輯此區塊

export interface Cookie

方法

說明

append(name, value, options)

使用 Set-Cookie 標頭附加 Response Cookie 標頭。

set()append() 的區別在於,如果指定的標頭已經存在,set() 會使用新值覆寫現有值,而 append() 會將新值附加到值集合的末尾。

delete(name, options)

使用 Response Cookie 標頭按名稱刪除 Cookie 值。

get(name)

按名稱取得 Request Cookie 標頭值。

getAll()

取得所有 Request Cookie 標頭。

has(name)

檢查 Request Cookie 標頭名稱是否存在。

headers()

返回所有已設置的 Response Set-Cookie 標頭值的陣列。

set(name, value, options)

使用 Set-Cookie 標頭設置 Response Cookie 標頭。

編輯此區塊

CookieOptions

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie

export interface CookieOptions

屬性

修飾符

類型

說明

domain?

string

(可選) 定義要將 Cookie 發送到哪個主機。如果省略,則此屬性預設為當前文檔 URL 的主機,不包括子網域。

expires?

Date | string

(可選) 表示 Cookie 的最大生存期,以 HTTP 日期時間戳記表示。如果同時設置了 expiresmaxAge,則 maxAge 優先。

httpOnly?

boolean

(可選) 禁止 JavaScript 訪問 Cookie,例如,通過 document.cookie 屬性。

maxAge?

number | [number, 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks']

(可選) 表示 Cookie 過期前的秒數。零或負數將立即使 Cookie 過期。如果同時設定了 expiresmaxAge,則 maxAge 優先。您也可以使用陣列語法來設定以分鐘、小時、天或週為單位的最長存在時間。例如,{ maxAge: [3, "days"] } 會將 Cookie 設定為在 3 天後過期。

path?

string

(可選) 表示請求 URL 中必須存在的路徑,瀏覽器才能發送 Cookie 標頭。

sameSite?

'strict' | 'lax' | 'none' | 'Strict' | 'Lax' | 'None' | boolean

(可選) 控制是否在跨站請求中發送 Cookie,提供一些針對跨站請求偽造攻擊 (CSRF) 的保護。

secure?

boolean

(可選) 表示僅當使用 https: 方案發出請求時,才會將 Cookie 發送到伺服器(localhost 除外)

編輯此區塊

CookieValue

export interface CookieValue

屬性

修飾符

類型

說明

json

<T = unknown>() => T

number

() => number

value

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(key)

編輯此區塊

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

CookieInterface

Headers

編輯此區塊

RedirectMessage

export declare class RedirectMessage extends AbortMessage

繼承自: AbortMessage

編輯此區塊

RequestEvent

export interface RequestEvent<PLATFORM = QwikCityPlatform> extends RequestEventCommon<PLATFORM>

繼承自: RequestEventCommon<PLATFORM>

屬性

修飾符

類型

說明

exited

readonly

boolean

如果中介軟體鏈已完成執行,則為 True。

getWritableStream

readonly

() => WritableStream<Uint8Array>

對寫入 HTTP 回應串流的低階存取權。呼叫 getWritableStream() 後,狀態和標頭將無法再修改,並將透過網路發送。

headersSent

readonly

boolean

如果已發送標頭,則為 True,防止設定任何其他標頭。

next

readonly

() => Promise<void>

呼叫鏈中的下一個中介軟體函數。

注意:確保對 next() 的呼叫已 await

編輯此區塊

RequestEventAction

export interface RequestEventAction<PLATFORM = QwikCityPlatform> extends RequestEventCommon<PLATFORM>

繼承自: RequestEventCommon<PLATFORM>

屬性

修飾符

類型

說明

fail

<T extends Record<string, any>>(status: number, returnData: T) => FailReturn<T>

編輯此區塊

RequestEventBase

export interface RequestEventBase<PLATFORM = QwikCityPlatform>

屬性

修飾符

類型

說明

basePathname

readonly

string

請求的基本路徑名稱,可以在建置時設定。預設為 /

cacheControl

readonly

(cacheControl: CacheControl, target?: CacheControlTarget) => void

設定快取控制標頭的便捷方法。根據您的 CDN,您可能需要新增另一個 cacheControl,並將第二個參數設定為 CDN-Cache-Control 或任何其他值(我們提供最常見的值以供自動完成,但您可以使用任何您想要的字串)。

如需更多資訊,請參閱 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Controlhttps://qwik.dev.org.tw/docs/caching/\#CDN-Cache-Controls

clientConn

readonly

ClientConn

提供有關客戶端連線的資訊,例如 IP 地址和請求來源國家/地區。

cookie

readonly

Cookie

HTTP 請求和回應 Cookie。使用 get() 方法擷取請求 Cookie 值。使用 set() 方法設定回應 Cookie 值。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies

env

readonly

EnvGetter

平台提供的環境變數。

headers

readonly

Headers

HTTP 回應標頭。請注意,在您第一次添加標頭之前,它將是空的。如果要讀取請求標頭,請改用 request.headers

https://developer.mozilla.org/en-US/docs/Glossary/Response\_header

method

readonly

string

HTTP 請求方法。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

params

readonly

Readonly<Record<string, string>>

已從當前 URL 路徑區段解析的 URL 路徑參數。使用 query 擷取查詢字串搜尋參數。

parseBody

readonly

() => Promise<unknown>

此方法將檢查請求標頭中是否有 Content-Type 標頭,並相應地解析主體。它支援 application/jsonapplication/x-www-form-urlencodedmultipart/form-data 內容類型。

如果未設定 Content-Type 標頭,它將返回 null

pathname

readonly

string

URL 路徑名稱。不包含通訊協定、網域、查詢字串(搜尋參數)或雜湊。

https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname

platform

readonly

PLATFORM

平台特定數據和函數

query

readonly

URLSearchParams

URL 查詢字串(URL 搜尋參數)。使用 params 擷取在 URL 路徑名稱中找到的路由參數。

https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

request

readonly

Request

HTTP 請求資訊。

sharedMap

readonly

Map<string, any>

所有請求處理常式共用的共用對應。每個 HTTP 請求都將取得共用對應的新執行個體。共用對應可用於在請求處理常式之間共用數據。

signal

readonly

AbortSignal

請求的 AbortSignal(與 request.signal 相同)。此信號表示請求已中止。

url

readonly

URL

HTTP 請求 URL。

編輯此區塊

RequestEventCommon

export interface RequestEventCommon<PLATFORM = QwikCityPlatform> extends RequestEventBase<PLATFORM>

繼承自: RequestEventBase<PLATFORM>

屬性

修飾符

類型

說明

error

readonly

(statusCode: ErrorCodes, message: string) => ErrorResponse

呼叫時,回應將立即以給定的狀態代碼結束。這對於以 404 結束回應並在路由目錄中使用 404 處理常式很有用。如需應使用哪個狀態代碼,請參閱 https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

exit

readonly

() => AbortMessage

html

readonly

(statusCode: StatusCodes, html: string) => AbortMessage

傳送 HTML 主體回應的便捷方法。回應將自動將 Content-Type 標頭設定為 text/html; charset=utf-8html() 回應只能呼叫一次。

json

readonly

(statusCode: StatusCodes, data: any) => AbortMessage

將數據 JSON 字串化並在回應中傳送的便捷方法。回應將自動將 Content-Type 標頭設定為 application/json; charset=utf-8json() 回應只能呼叫一次。

locale

readonly

(local?: string) => string

內容使用的地區設定。

可以使用 getLocale() 從選取的方法中擷取地區設定值

redirect

readonly

(statusCode: RedirectCode, url: string) => RedirectMessage

重新導向的網址。呼叫時,回應將立即以正確的重新導向狀態和標頭結束。

https://developer.mozilla.org/zh-TW/docs/Web/HTTP/Redirections

傳送

readonly

SendMethod

傳送主體回應。使用 send() 時,不會自動設定 Content-Type 回應標頭,必須手動設定。send() 回應只能呼叫一次。

status

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-8text() 回應只能呼叫一次。

編輯此區塊

RequestEventLoader

export interface RequestEventLoader<PLATFORM = QwikCityPlatform> extends RequestEventAction<PLATFORM>

繼承: RequestEventAction<PLATFORM>

屬性

修飾符

類型

說明

延遲

<T>(returnData: Promise<T> | (() => Promise<T>)) => DeferReturn<T>

解析值

ResolveValue

編輯此區塊

請求處理常式

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

繼承: 錯誤

建構函式

修飾符

說明

(constructor)(status, data)

建構 ServerError 類別的新執行個體

屬性

修飾符

類型

說明

資料

T

status

number

編輯此區塊

伺服器渲染選項

export interface ServerRenderOptions extends RenderOptions

繼承: RenderOptions

屬性

修飾符

類型

說明

checkOrigin?

boolean

(可選) 防止跨站請求偽造 (CSRF) 攻擊的保護。

true 時,對於每個傳入的 POST、PUT、PATCH 或 DELETE 表單提交,會檢查請求來源是否與伺服器的來源相符。

停用此選項時請小心,因為這可能會導致 CSRF 攻擊。

預設值為 true

qwikCityPlan

QwikCityPlan

渲染

Render

編輯此區塊

伺服器請求事件

由伺服器建立的請求事件。

export interface ServerRequestEvent<T = unknown>

屬性

修飾符

類型

說明

env

EnvGetter

取得用戶端連線

() => ClientConn

getWritableStream

ServerResponseHandler<T>

locale

字串 | 未定義

模式

ServerRequestMode

platform

QwikCityPlatform

request

Request

url

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

CookieOptions

(可選)

void

狀態

status: number;