# [API](/api/) › @qwik.dev/qwik-router/middleware/request-handler

## __controlFlow

Type-only brand so `Exclude<>` can drop control-flow signals from resolved loader/action data.

```typescript
readonly __controlFlow: true;
```

## AbortMessage

```typescript
export declare class AbortMessage
```

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [\_\_controlFlow](#abortmessage-__controlflow) | `readonly` | true | Type-only brand so `Exclude<>` can drop control-flow signals from resolved loader/action data. |

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/redirect-handler.ts)

## append

Appends a `Response` cookie header using the `Set-Cookie` header.

The difference between `set()` and `append()` is that if the specified header already exists, `set()` will overwrite the existing value with the new one, whereas `append()` will append the new value onto the end of the set of values.

```typescript
append(name: string, value: string | number | Record<string, any>, options?: CookieOptions): void;
```

| Parameter | Type | Description |
| --- | --- | --- |
| name | string |  |
| value | string \\| number \\| Record<string, any> |  |
| options | [CookieOptions](#cookieoptions) | _(Optional)_ |

**Returns:**

void

## CacheControl

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

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## clearLoaderCache

Clear the in-memory loader cache. Call after deployments or data changes.

When `cacheKey` is provided, only that single entry is removed; otherwise the entire cache is cleared.

```typescript
export declare function clearLoaderCache(cacheKey?: string): void;
```

| Parameter | Type | Description |
| --- | --- | --- |
| cacheKey | string | _(Optional)_ |

**Returns:**

void

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/etag.ts)

## clearSsrCache

Clear the in-memory SSR cache. Call after deployments or data changes.

When `cacheKey` is provided, only that single entry is removed; otherwise the entire cache is cleared.

```typescript
export declare function clearSsrCache(cacheKey?: string): void;
```

| Parameter | Type | Description |
| --- | --- | --- |
| cacheKey | string | _(Optional)_ |

**Returns:**

void

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/etag.ts)

## ClientConn

```typescript
export interface ClientConn
```

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| country? |  | string | _(Optional)_ |
| ip? |  | string | _(Optional)_ |

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## Cookie

```typescript
export interface Cookie
```

| Method | Description |
| --- | --- |
| [append(name, value, options)](#cookie-append) | Appends a `Response` cookie header using the `Set-Cookie` header. The difference between `set()` and `append()` is that if the specified header already exists, `set()` will overwrite the existing value with the new one, whereas `append()` will append the new value onto the end of the set of values. |
| [delete(name, options)](#cookie-delete) | Deletes cookie value by name using the `Response` cookie header. |
| [get(name)](#cookie-get) | Gets a `Request` cookie header value by name. |
| [getAll()](#cookie-getall) | Gets all `Request` cookie headers. |
| [has(name)](#cookie-has) | Checks if the `Request` cookie header name exists. |
| [headers()](#cookie-headers) | Returns an array of all the set `Response` `Set-Cookie` header values. |
| [set(name, value, options)](#cookie-set) | Sets a `Response` cookie header using the `Set-Cookie` header. |

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## CookieOptions

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

```typescript
export interface CookieOptions
```

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| domain? |  | string | _(Optional)_ Defines the host to which the cookie will be sent. If omitted, this attribute defaults to the host of the current document URL, not including subdomains. |
| expires? |  | Date \\| string | _(Optional)_ Indicates the maximum lifetime of the cookie as an HTTP-date timestamp. If both `expires` and `maxAge` are set, `maxAge` has precedence. |
| httpOnly? |  | boolean | _(Optional)_ Forbids JavaScript from accessing the cookie, for example, through the `document.cookie` property. |
| maxAge? |  | number \\| [number, 'seconds' \\| 'minutes' \\| 'hours' \\| 'days' \\| 'weeks'] | _(Optional)_ Indicates the number of seconds until the cookie expires. A zero or negative number will expire the cookie immediately. If both `expires` and `maxAge` are set, `maxAge` has precedence. You can also use the array syntax to set the max-age using minutes, hours, days or weeks. For example, `{ maxAge: [3, "days"] }` would set the cookie to expire in 3 days. |
| path? |  | string | _(Optional)_ Indicates the path that must exist in the requested URL for the browser to send the Cookie header. |
| sameSite? |  | 'strict' \\| 'lax' \\| 'none' \\| 'Strict' \\| 'Lax' \\| 'None' \\| boolean | _(Optional)_ Controls whether or not a cookie is sent with cross-site requests, providing some protection against cross-site request forgery attacks (CSRF). |
| secure? |  | boolean | _(Optional)_ Indicates that the cookie is sent to the server only when a request is made with the `https:` scheme (except on localhost) |

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## CookieValue

```typescript
export interface CookieValue
```

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| json |  | <T = unknown>() => T |  |
| number |  | () => number |  |
| value |  | string |  |

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## data

```typescript
data: T;
```

## DeferReturn

```typescript
export type DeferReturn<T> = () => Promise<T>;
```

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## delete

Deletes cookie value by name using the `Response` cookie header.

```typescript
delete(name: string, options?: Pick<CookieOptions, 'path' | 'domain' | 'sameSite'>): void;
```

| Parameter | Type | Description |
| --- | --- | --- |
| name | string |  |
| options | Pick<[CookieOptions](#cookieoptions), 'path' \\| 'domain' \\| 'sameSite'> | _(Optional)_ |

**Returns:**

void

## EnvGetter

```typescript
export interface EnvGetter
```

| Method | Description |
| --- | --- |
| get(key) |  |

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## get

Gets a `Request` cookie header value by name.

```typescript
get(name: string): CookieValue | null;
```

| Parameter | Type | Description |
| --- | --- | --- |
| name | string |  |

**Returns:**

[CookieValue](#cookievalue) \| null

## getAll

Gets all `Request` cookie headers.

```typescript
getAll(): Record<string, CookieValue>;
```

**Returns:**

Record&lt;string, [CookieValue](#cookievalue)&gt;

## getErrorHtml

```typescript
export declare function getErrorHtml(status: number, e: any): string;
```

| Parameter | Type | Description |
| --- | --- | --- |
| status | number |  |
| e | any |  |

**Returns:**

string

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/error-handler.ts)

## has

Checks if the `Request` cookie header name exists.

```typescript
has(name: string): boolean;
```

| Parameter | Type | Description |
| --- | --- | --- |
| name | string |  |

**Returns:**

boolean

## headers

Returns an array of all the set `Response` `Set-Cookie` header values.

```typescript
headers(): string[];
```

**Returns:**

string[]

## InternalRequest

Internal JSON request kind handled by Qwik Router, or `false` for normal page requests.

```typescript
export type InternalRequest = false | "loader" | "action";
```

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## mergeHeadersCookies

```typescript
mergeHeadersCookies: (headers: Headers, cookies: CookieInterface) => Headers;
```

| Parameter | Type | Description |
| --- | --- | --- |
| headers | Headers |  |
| cookies | [CookieInterface](#cookie) |  |

**Returns:**

Headers

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/cookie.ts)

## pathname

```typescript
readonly pathname: string;
```

## RedirectMessage

```typescript
export declare class RedirectMessage extends AbortMessage
```

**Extends:** [AbortMessage](#abortmessage)

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/redirect-handler.ts)

## RequestEvent

```typescript
export interface RequestEvent<PLATFORM = QwikRouterPlatform> extends RequestEventCommon<PLATFORM>
```

**Extends:** [RequestEventCommon](#requesteventcommon)&lt;PLATFORM&gt;

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| exited | `readonly` | boolean | True if the middleware chain has finished executing. |
| getWritableStream | `readonly` | () => WritableStream<Uint8Array> | Low-level access to write to the HTTP response stream. Once `getWritableStream()` is called, the status and headers can no longer be modified and will be sent over the network. |
| headersSent | `readonly` | boolean | True if headers have been sent, preventing any more headers from being set. |
| next | `readonly` | () => Promise<void> | Invoke the next middleware function in the chain. NOTE: Ensure that the call to `next()` is `await`ed. |

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## RequestEventAction

```typescript
export interface RequestEventAction<PLATFORM = QwikRouterPlatform> extends RequestEventCommon<PLATFORM>
```

**Extends:** [RequestEventCommon](#requesteventcommon)&lt;PLATFORM&gt;

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| fail |  | <T extends Record<string, any>>(status: number, returnData: T) => FailReturn<T> |  |

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## RequestEventBase

```typescript
export interface RequestEventBase<PLATFORM = QwikRouterPlatform>
```

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| basePathname | `readonly` | string | The base pathname of the request, which can be configured at build time. Defaults to `/`. |
| cacheControl | `readonly` | (cacheControl: [CacheControl](#cachecontrol), target?: CacheControlTarget) => void | Convenience method to set the Cache-Control header. Depending on your CDN, you may want to add another cacheControl with the second argument set to `CDN-Cache-Control` or any other value (we provide the most common values for auto-complete, but you can use any string you want). See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control and https://qwik.dev/docs/caching/\#CDN-Cache-Controls for more information. |
| clientConn | `readonly` | [ClientConn](#clientconn) | Provides information about the client connection, such as the IP address and the country the request originated from. |
| cookie | `readonly` | [Cookie](#cookie) | HTTP request and response cookie. Use the `get()` method to retrieve a request cookie value. Use the `set()` method to set a response cookie value. https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies |
| env | `readonly` | [EnvGetter](#envgetter) | Platform provided environment variables. |
| headers | `readonly` | Headers | HTTP response headers. Notice it will be empty until you first add a header. If you want to read the request headers, use `request.headers` instead. https://developer.mozilla.org/en-US/docs/Glossary/Response\_header |
| internalRequest | `readonly` | [InternalRequest](#internalrequest) | Identifies Qwik Router internal JSON requests, such as route loader fetches and fetch-based action submissions. Returns `false` for normal page requests. Check this before applying broad rewrite rules so internal JSON requests keep their routing behavior. |
| method | `readonly` | string | HTTP request method. https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods |
| originalUrl | `readonly` | URL | The original HTTP request URL. This property was introduced to support the rewrite feature. If rewrite is called, the url property will be changed to the rewritten url. while originalUrl will stay the same(e.g the url inserted to the address bar). If rewrite is never called as part of the request, the url property and the originalUrl are equal. |
| params | `readonly` | Readonly<Record<string, string>> | URL path params which have been parsed from the current url pathname segments. Use `query` to instead retrieve the query string search params. |
| parseBody | `readonly` | () => Promise<unknown> | This method will check the request headers for a `Content-Type` header and parse the body accordingly. It supports `application/json`, `application/x-www-form-urlencoded`, and `multipart/form-data` content types. If the `Content-Type` header is not set, it will return `null`. |
| pathname | `readonly` | string | URL pathname. Does not include the protocol, domain, query string (search params) or hash. https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname |
| platform | `readonly` | PLATFORM | Platform specific data and functions |
| query | `readonly` | URLSearchParams | URL Query Strings (URL Search Params). Use `params` to instead retrieve the route params found in the url pathname. https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams |
| request | `readonly` | Request | HTTP request information. |
| sharedMap | `readonly` | Map<string, any> | Shared Map across all the request handlers. Every HTTP request will get a new instance of the shared map. The shared map is useful for sharing data between request handlers. |
| signal | `readonly` | AbortSignal | Request's AbortSignal (same as `request.signal`). This signal indicates that the request has been aborted. |
| url | `readonly` | URL | HTTP request URL. |

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## RequestEventCommon

```typescript
export interface RequestEventCommon<PLATFORM = QwikRouterPlatform> extends RequestEventBase<PLATFORM>
```

**Extends:** [RequestEventBase](#requesteventbase)&lt;PLATFORM&gt;

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| error | `readonly` | <T = any>(statusCode: ErrorCodes, message: T) => [ServerError](#servererror)<T> | When called, the response will immediately end with the given status code. This could be useful to end a response with `404`, and use the 404 handler in the routes directory. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status for which status code should be used. |
| exit | `readonly` | () => [AbortMessage](#abortmessage) |  |
| html | `readonly` | (statusCode: StatusCodes, html: string) => [AbortMessage](#abortmessage) | Convenience method to send an HTML body response. The response will be automatically set the `Content-Type` header to`text/html; charset=utf-8`. An `html()` response can only be called once. |
| json | `readonly` | (statusCode: StatusCodes, data: any) => [AbortMessage](#abortmessage) | Convenience method to JSON stringify the data and send it in the response. The response will be automatically set the `Content-Type` header to `application/json; charset=utf-8`. A `json()` response can only be called once. |
| locale | `readonly` | (local?: string) => string | Which locale the content is in. The locale value can be retrieved from selected methods using `getLocale()`: |
| redirect | `readonly` | (statusCode: RedirectCode, url: string) => [RedirectMessage](#redirectmessage) | URL to redirect to. When called, the response will immediately end with the correct redirect status and headers. https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections |
| rewrite | `readonly` | (pathname: string) => [RewriteMessage](#rewritemessage) | When called, qwik-router will execute the path's matching route flow. The url in the browser will remain unchanged. |
| send | `readonly` | SendMethod | Send a body response. The `Content-Type` response header is not automatically set when using `send()` and must be set manually. A `send()` response can only be called once. |
| status | `readonly` | (statusCode?: StatusCodes) => number | HTTP response status code. Sets the status code when called with an argument. Always returns the status code, so calling `status()` without an argument will can be used to return the current status code. https://developer.mozilla.org/en-US/docs/Web/HTTP/Status |
| text | `readonly` | (statusCode: StatusCodes, text: string) => [AbortMessage](#abortmessage) | Convenience method to send an text body response. The response will be automatically set the `Content-Type` header to`text/plain; charset=utf-8`. An `text()` response can only be called once. |

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## RequestEventLoader

```typescript
export interface RequestEventLoader<PLATFORM = QwikRouterPlatform> extends RequestEventAction<PLATFORM>
```

**Extends:** [RequestEventAction](#requesteventaction)&lt;PLATFORM&gt;

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| defer |  | <T>(returnData: Promise<T> \\| (() => Promise<T>)) => [DeferReturn](#deferreturn)<T> |  |
| resolveValue |  | [ResolveValue](#resolvevalue) |  |

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## requestHandler

```typescript
export type RequestHandler<PLATFORM = QwikRouterPlatform> = (
  ev: RequestEvent<PLATFORM>,
) =>
  | Promise<void | AbortMessage | ServerError>
  | void
  | AbortMessage
  | ServerError;
```

**References:** [RequestEvent](#requestevent), [AbortMessage](#abortmessage), [ServerError](#servererror)

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/request-handler.ts)

## RequestHandler

```typescript
export type RequestHandler<PLATFORM = QwikRouterPlatform> = (
  ev: RequestEvent<PLATFORM>,
) =>
  | Promise<void | AbortMessage | ServerError>
  | void
  | AbortMessage
  | ServerError;
```

**References:** [RequestEvent](#requestevent), [AbortMessage](#abortmessage), [ServerError](#servererror)

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## ResolveSyncValue

```typescript
export interface ResolveSyncValue
```

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## ResolveValue

```typescript
export interface ResolveValue
```

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## RewriteMessage

```typescript
export declare class RewriteMessage extends AbortMessage
```

**Extends:** [AbortMessage](#abortmessage)

| Constructor | Modifiers | Description |
| --- | --- | --- |
| (constructor)(pathname) |  | Constructs a new instance of the `RewriteMessage` class |

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [pathname](#rewritemessage-pathname) | `readonly` | string |  |

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/rewrite-handler.ts)

## ServerError

```typescript
export declare class ServerError<T = any> extends Error
```

**Extends:** Error

| Constructor | Modifiers | Description |
| --- | --- | --- |
| (constructor)(status, data) |  | Constructs a new instance of the `ServerError` class |

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [data](#servererror-data) |  | T |  |
| [status](#servererror-status) |  | number |  |

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/server-error.ts)

## ServerRenderOptions

```typescript
export interface ServerRenderOptions extends RenderOptions
```

**Extends:** RenderOptions

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| checkOrigin? |  | boolean \\| 'lax-proto' | _(Optional)_ Protection against cross-site request forgery (CSRF) attacks. When `true`, for every incoming POST, PUT, PATCH, or DELETE form submissions, the request origin is checked to match the server's origin. `lax-proto` is for SSL-terminating proxies Be careful when disabling this option as it may lead to CSRF attacks. Defaults to `true`. |
| render |  | Render |  |

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## ServerRequestEvent

Request event created by the server.

```typescript
export interface ServerRequestEvent<T = unknown>
```

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| env |  | [EnvGetter](#envgetter) |  |
| getClientConn |  | () => [ClientConn](#clientconn) |  |
| getWritableStream |  | [ServerResponseHandler](#serverresponsehandler)<T> |  |
| locale |  | string \\| undefined |  |
| mode |  | [ServerRequestMode](#serverrequestmode) |  |
| platform |  | QwikRouterPlatform |  |
| request |  | Request |  |
| url |  | URL |  |

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## ServerRequestMode

```typescript
export type ServerRequestMode = "static" | "server";
```

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## ServerResponseHandler

```typescript
export type ServerResponseHandler<T = any> = (
  status: number,
  headers: Headers,
  cookies: Cookie,
  resolve: (response: T) => void,
  requestEv: RequestEvent,
) => WritableStream<Uint8Array>;
```

**References:** [Cookie](#cookie), [RequestEvent](#requestevent)

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-router/src/middleware/request-handler/types.ts)

## set

Sets a `Response` cookie header using the `Set-Cookie` header.

```typescript
set(name: string, value: string | number | Record<string, any>, options?: CookieOptions): void;
```

| Parameter | Type | Description |
| --- | --- | --- |
| name | string |  |
| value | string \\| number \\| Record<string, any> |  |
| options | [CookieOptions](#cookieoptions) | _(Optional)_ |

**Returns:**

void

## status

```typescript
status: number;
```
