Skip to main content
Module

x/oak/mod.ts

A middleware framework for handling HTTP with Deno, Node, Bun and Cloudflare Workers 🐿️ 🦕
Extremely Popular
Latest
import * as oak from "https://deno.land/x/oak@v14.1.1/mod.ts";

A middleware framework for handling HTTP with Deno.

oak works well on both Deno CLI and Deno deploy, and is inspired by koa. It works well with both the Deno CLI and Deno Deploy.

Example server

A minimal router server which responds with content on /. With Deno CLI this will listen on port 8080 and on Deploy, this will simply serve requests received on the application.

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

const router = new Router();
router.get("/", (ctx) => {
  ctx.response.body = `<!DOCTYPE html>
    <html>
      <head><title>Hello oak!</title><head>
      <body>
        <h1>Hello oak!</h1>
      </body>
    </html>
  `;
});

const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());

app.listen({ port: 8080 });

Namespaces

A collection of oak specific APIs for management of ETags.

A collection of APIs to help assist in creating middleware.

A collection of utility APIs which can make testing of an oak application easier.

Classes

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

Provides context about the current request and response to middleware functions, and the current instance being processed is the first argument provided a Middleware function.

Provides an way to manage cookies in a request and response on the server as a single iterable collection, as well as the ability to sign and verify cookies to prevent tampering.

The base class that all derivative HTTP extend, providing a status and an expose property.

The oak abstraction of the Deno native HTTP server which is used internally for handling native HTTP requests. Generally users of oak do not need to worry about this class.

A class that takes a file (either a Deno.FsFile or Uint8Array) and bytes and streams the ranges as a multi-part encoded HTTP body.

An internal oak abstraction for handling a Deno native request. Most users of oak do not need to worry about this abstraction.

An interface which provides information about the current request. The instance related to the current request is available on the Context's .request property.

An interface to control what response will be sent when the middleware finishes processing the request.

The context associated with serve middleware requests on a router.

An interface for registering middleware that will run when certain HTTP methods and paths are requested, as well as provides a way to parameterize parts of the requested path.

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

An event which contains information which will be sent to the remote connection and be made available in an EventSource as an event. A server creates new events and dispatches them on the target which will then be sent to a client.

Enums

Standard HTTP status codes.

Variables

A namespace that contains each error constructor. Each error extends HTTPError and provides .status and .expose properties, where the .status will be an error Status value and .expose indicates if information, like a stack trace, should be shared in the response.

A symbol that indicates to response.redirect() to attempt to redirect back to the request referrer. For example:

A record of all the status codes text.

Allows external parties to modify the context state.

Functions

Compose multiple middleware functions into a single middleware function.

A factory function which provides a way to create errors. It takes up to 3 arguments, the error Status, an message, which defaults to the status text and error options, which includes the expose property to set the .expose value on the error.

Calculate an ETag for an entity. When the entity is a specific set of data it will be fingerprinted as a "strong" tag, otherwise if it is just file information, it will be calculated as a weak tag.

Create middleware that will attempt to decode the response.body into something that can be used to generate an ETag and add the ETag header to the response.

For a given Context, try to determine the response body entity that an ETag can be calculated from.

A helper function that takes the value from the If-Match header and a calculated etag for the target. By using strong comparison, return true if the values match, otherwise false.

A helper function that takes the value from the If-None-Match header and a calculated etag for the target entity and returns false if the etag for the entity matches the supplied value, otherwise true.

Given a context, return the .request.url.searchParams as a Map of keys and values of the params.

Determine, by the value of an If-Range header, if a Range header should be applied to a request, returning true if it should or otherwise false.

A type guard that determines if the status code is an error.

A type guard that determines if the value is an HttpError or not.

A type guard that determines if the status code is a redirection.

Given a range header value and a current byte size of an asset, determine what ranges of bytes are being requested.

Middleware that provides a back-to-back proxy for requests.

Wrap a handler function to generate middleware that can be used with an oak Router. This allows the handler to deal with a Fetch API standard Request and return a standard Response.

Asynchronously fulfill a response with a file from the local file system.

Wrap a handler function to generate middleware that can be used with an oak Application. This allows the handler to deal with a Fetch API standard Request and return a standard Response.

Creates a mock of Application.

Create a mock of Context or RouterContext.

Creates a mock next() function which can be used when calling middleware.

Interfaces

Available options that are used when creating a new instance of Application.

A representation of range of bytes.

Options that can be supplied when using the .send() method.

Options for calculate.

Just the part of Deno.FileInfo that is required to calculate an ETag, so partial or user generated file information can be passed.

Base interface for application listening options.

Interface options when listening on TLS.

Middleware are functions which are chained together to deal with requests.

Middleware objects allow encapsulation of middleware along with the ability to initialize the middleware upon listen.

Options which can be specified on the proxy middleware.

The internal abstraction of a route used by the oak Router.

Options which can be specified when calling the .allowedMethods() method on a Router instance.

The context passed router middleware.

The interface that Router middleware should adhere to.

Options which can be specified when creating a new instance of a Router.

Middleware that will be called by the router when handling a specific parameter, which the middleware will be called when a request matches the route parameter.

Options which can be specified when using the send middleware.

The abstract constructor interface that custom servers need to adhere to when using with an Application.

Options that can be set in a mock context.

Type Aliases

An HTTP status that is an error (4XX and 5XX).

Options which can be specified when using getQuery.

A type representing string literals of each of the common HTTP method.

Options which can be specified when listening.

Type that represents Middleware or MiddlewareObject.

A function for chaining middleware.

An HTTP status that is a redirect (3XX).

A dynamic type which attempts to determine the route params based on matching the route string.

The base type of state which is associated with an application or context.