Hi there! Are you looking for the official Deno documentation? Try docs.deno.com for all your Deno learning needs.

Application

import { Application } from "https://raw.githubusercontent.com/mandarineorg/mandarinets/master/deps.ts";

A class which registers middleware (via .use()) and then processes inbound requests against that middleware (via .listen()).

The context.state can be typed via passing a generic argument when constructing an instance of Application.

class Application<AS extends State = Record<string, any>> extends EventTarget {
constructor(options?: ApplicationOptions<AS>);
handle;
get keys(): KeyStack | Key[] | undefined;
set keys(keys: KeyStack | Key[] | undefined);
proxy: boolean;
state: AS;
 
addEventListener(
type: "error",
listener: ApplicationErrorEventListenerOrEventListenerObject<AS> | null,
options?: boolean | AddEventListenerOptions,
): void;
addEventListener(
type: "listen",
listener: ApplicationListenEventListenerOrEventListenerObject | null,
options?: boolean | AddEventListenerOptions,
): void;
addEventListener(
type: "error" | "listen",
listener: EventListenerOrEventListenerObject | null,
options?: boolean | AddEventListenerOptions,
): void;
fetchEventHandler(): FetchEventListenerObject;
async listen(addr: string): Promise<void>;
async listen(options: ListenOptions): Promise<void>;
async listen(options: string | ListenOptions): Promise<void>;
use<S extends State = AS>(...middleware: Middleware<S, Context<S>>[]): Application<S extends AS ? S : (S & AS)>;
}

§Type Parameters

§
AS extends State = Record<string, any>
[src]

§Extends

§
EventTarget
[src]

§Constructors

§
new Application(options?: ApplicationOptions<AS>)
[src]

§Properties

§
handle
[src]

Handle an individual server request, returning the server response. This is similar to .listen(), but opening the connection and retrieving requests are not the responsibility of the application. If the generated context gets set to not to respond, then the method resolves with undefined, otherwise it resolves with a request that is compatible with std/http/server.

§
keys: KeyStack | Key[] | undefined
[src]

A set of keys, or an instance of KeyStack which will be used to sign cookies read and set by the application to avoid tampering with the cookies.

§
proxy: boolean
[src]

If true, proxy headers will be trusted when processing requests. This defaults to false.

§
state: AS
[src]

Generic state of the application, which can be specified by passing the generic argument when constructing:

  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" } });

When a new context is created, the application's state is cloned and the state is unique to that request/response. Changes can be made to the application state that will be shared with all contexts.

§Methods

§
addEventListener(type: "error", listener: ApplicationErrorEventListenerOrEventListenerObject<AS> | null, options?: boolean | AddEventListenerOptions): void
[src]

Add an event listener for an "error" event which occurs when an un-caught error occurs when processing the middleware or during processing of the response.

addEventListener(type: "listen", listener: ApplicationListenEventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void
[src]

Add an event listener for a "listen" event which occurs when the server has successfully opened but before any requests start being processed.

addEventListener(type: "error" | "listen", listener: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void
[src]

Add an event listener for an event. Currently valid event types are "error" and "listen".

§
fetchEventHandler(): FetchEventListenerObject
[src]

When using Deno Deploy, this method can create an event handler object for the application which can be registered as a fetch event handler.

import { Application } from "https://deno.land/x/oak/mod.ts";

const app = new App();
app.use((ctx) => ctx.response.body = "hello oak");

addEventListener("fetch", app.fetchEventHandler());
§
listen(addr: string): Promise<void>
[src]

Start listening for requests, processing registered middleware on each request. If the options .secure is undefined or false, the listening will be over HTTP. If the options .secure property is true, a .certFile and a .keyFile property need to be supplied and requests will be processed over HTTPS.

listen(options: ListenOptions): Promise<void>
[src]

Start listening for requests, processing registered middleware on each request. If the options .secure is undefined or false, the listening will be over HTTP. If the options .secure property is true, a .certFile and a .keyFile property need to be supplied and requests will be processed over HTTPS.

listen(options: string | ListenOptions): Promise<void>
[src]
§
use<S extends State = AS>(...middleware: Middleware<S, Context<S>>[]): Application<S extends AS ? S : (S & AS)>
[src]

Register middleware to be used with the application. Middleware will be processed in the order it is added, but middleware can control the flow of execution via the use of the next() function that the middleware function will be called with. The context object provides information about the current state of the application.

Basic usage:

const import { Application } from "https://deno.land/x/oak/mod.ts";

const app = new Application();

app.use((ctx, next) => {
  ctx.request; // contains request information
  ctx.response; // setups up information to use in the response;
  await next(); // manages the flow control of the middleware execution
});

await app.listen({ port: 80 });