Skip to main content
Module

x/acorn/context.ts>Context

A focused RESTful server framework for Deno 🌰🦕
Latest
class Context
import { Context } from "https://deno.land/x/acorn@0.5.1/context.ts";

An object that provides context for the associated request and response. This is passed as the first argument to every route handler.

Constructors

new
Context(unnamed 0: ContextOptions<BodyType, Params>)

Type Parameters

optional
BodyType = unknown
optional
Params extends Record<string, string> = Record<string, string>

Properties

readonly
addr: Addr

The address this request.

readonly
cookies: SecureCookieMap

The instance of Cookies that allows reading and setting of cookies on the request and response.

readonly
params: Params

Any Params that have been parsed out of the URL requested based on the URL pattern string provided to the Route.

readonly
request: Request

The original Request associated with this request.

readonly
responded: boolean

Indicates if the response has already been responded to, like when upgrading to a websocket.

readonly
searchParams: Record<string, string>

Any search parameters associated with the request.

readonly
userAgent: UserAgent

Information about the parsed user agent string associated with the Request if available.

See: std/http/user_agent#UserAgent for more information.

Methods

body(): Promise<BodyType | undefined>

A convenience method to deal with decoding a JSON string body. It can be used with an optional Deserializer which can do advanced decoding of the body, or it will attempted to be decoded from the JSON string.

Attempt to upgrade the request to a web socket, returning the socket and the response to be returned.

Example

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

const router = new Router();

router.get("/ws", (ctx) => {
  const { socket, response } = ctx.upgrade();
  // Perform actions with the socket.
  return response;
});

router.listen({ port: 8000 });
url(): URL

Returns the request URL as a parsed URL object.