Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/oak/middleware/serve.ts>ServeContext

A middleware framework for handling HTTP with Deno 🐿️ 🦕
Very Popular
Go to Latest
class ServeContext
import { ServeContext } from "https://deno.land/x/oak@v14.1.0/middleware/serve.ts";

The context associated when dealing with serve middleware requests on an application.

Constructors

new
ServeContext(context: Context<S>)

Type Parameters

optional
S extends State = State

Properties

readonly
app: Application<S>

A reference to the current application.

readonly
ip: string

Request remote address. When the application's .proxy is true, the X-Forwarded-For will be used to determine the requesting remote address.

readonly
ips: string[]

When the application's .proxy is true, this will be set to an array of IPs, ordered from upstream to downstream, based on the value of the header X-Forwarded-For. When false an empty array is returned.

readonly
state: S

The object to pass state to front-end views. This can be typed by supplying the generic state argument when creating a new app. For example:

const app = new Application<{ foo: string }>();

Or can be contextually inferred based on setting an initial state object:

const app = new Application({ state: { foo: "bar" } });

On each request/response cycle, the context's state is cloned from the application state. This means changes to the context's .state will be dropped when the request drops, but "defaults" can be applied to the application's state. Changes to the application's state though won't be reflected until the next request in the context's state.

Methods

assert(
condition: unknown,
status?: ErrorStatus,
message?: string,
props?: Record<string, unknown> & Omit<HttpErrorOptions, "status">,
): asserts condition

Asserts the condition and if the condition fails, creates an HTTP error with the provided status (which defaults to 500). The error status by default will be set on the .response.status.

Because of limitation of TypeScript, any assertion type function requires specific type annotations, so the ServeContext type should be used even if it can be inferred from the context.

throw(
errorStatus: ErrorStatus,
message?: string,
props?: Record<string, unknown>,
): never

Create and throw an HTTP Error, which can be used to pass status information which can be caught by other middleware to send more meaningful error messages back to the client. The passed error status will be set on the .response.status by default as well.

[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string): string
[Symbol.for("nodejs.util.inspect.custom")](
depth: number,
options: any,
inspect: (value: unknown, options?: unknown) => string,
): any