Skip to main content
Module

x/opine/mod.ts>OpineRequest

Minimalist web framework for Deno ported from ExpressJS.
Latest
interface OpineRequest
implements [Opine.Request]
import { type OpineRequest } from "https://deno.land/x/opine@2.3.4/mod.ts";

Examples

app.get('/user/:id', (req, res) => res.send(req.params.id)); // implicitly ParamsDictionary app.get(/user/(.)/, (req, res) => res.send(req.params[0])); app.get('/user/', (req, res) => res.send(req.params[0]));

Type Parameters

optional
P extends Params = ParamsDictionary
optional
ResBody = any
optional
ReqQuery = any

Properties

get: (name: string) => string | undefined

Return request header.

The Referrer header field is special-cased, both Referrer and Referer are interchangeable.

Examples:

req.get('Content-Type');
// => "text/plain"

req.get('content-type');
// => "text/plain"

req.get('Something');
// => undefined
param: (name: string, defaultValue?: string) => string | undefined
protocol: string

Return the protocol string "http" or "https" when requested with TLS. When the "trust proxy" setting is enabled the "X-Forwarded-Proto" header field will be trusted. If you're running behind a reverse proxy that supplies https for you this may be enabled.

secure: boolean

Short-hand for:

req.protocol == 'https'

ip: string

Return the remote address, or when "trust proxy" is true return the upstream addr.

ips: string[]

When "trust proxy" is true, parse the "X-Forwarded-For" ip address list.

For example if the value were "client, proxy1, proxy2" you would receive the array ["client", "proxy1", "proxy2"] where "proxy2" is the furthest down-stream.

subdomains: string[]

Return subdomains as an array.

Subdomains are the dot-separated parts of the host before the main domain of the app. By default, the domain of the app is assumed to be the last two parts of the host. This can be changed by setting "subdomain offset".

For example, if the domain is "deno.dinosaurs.example.com": If "subdomain offset" is not set, req.subdomains is ["dinosaurs", "deno"]. If "subdomain offset" is 3, req.subdomains is ["deno"].

path: string

Returns the pathname of the URL.

optional
hostname: string

Parse the "Host" header field hostname.

fresh: boolean

Check if the request is fresh, aka Last-Modified and/or the ETag still match.

stale: boolean

Check if the request is stale, aka "Last-Modified" and / or the "ETag" for the resource has changed.

xhr: boolean

Check if the request was an XMLHttpRequest.

body: any

Body of request.

If the body has been passed such that a parsedBody property is defined, this returns the parsedBody value.

To always get the raw body value, use the raw property.

Raw body of request.

method: string
params: P
query: ReqQuery
route: any
originalUrl: string
url: string
baseUrl: string
proto: string
headers: Headers
conn: ConnInfo
optional
res: OpineResponse<ResBody>

After middleware.init executed, OpineRequest will contain res and next properties. See: opine/src/middleware/init.ts

optional
next: NextFunction
optional
_parsedBody: boolean

After body parsers, OpineRequest will contain _parsedBody boolean property dictating that the body has been parsed. See: opine/src/middleware/bodyParser/

optional
parsedBody: any

After body parsers, OpineRequest will contain parsedBody property containing the parsed body. See: opine/src/middleware/bodyParser/

optional
_parsedUrl: ParsedURL

After calling parseUrl on the request object, the parsed url is memoization by storing onto the _parsedUrl property.

optional
_parsedOriginalUrl: ParsedURL

After calling originalUrl on the request object, the original url is memoization by storing onto the _parsedOriginalUrl property.

finalResponse: Promise<Response>

Returns a promise that resolves to the response to the request.

Methods

accepts(): string | string[] | false

Check if the given type(s) is acceptable, returning the best match when true, otherwise undefined, in which case you should respond with 406 "Not Acceptable".

The type value may be a single mime type string such as "application/json", the extension name such as "json", a comma-delimited list such as "json, html, text/plain", or an array ["json", "html", "text/plain"]. When a list or array is given the best match, if any is returned.

Examples:

// Accept: text/html
req.accepts('html');
// => "html"

// Accept: text/*, application/json
req.accepts('html');
// => "html"
req.accepts('text/html');
// => "text/html"
req.accepts('json, text');
// => "json"
req.accepts('application/json');
// => "application/json"

// Accept: text/*, application/json
req.accepts('image/png');
req.accepts('png');
// => undefined

// Accept: text/*;q=.5, application/json
req.accepts(['html', 'json']);
req.accepts('html, json');
// => "json"
accepts(type: string): string | string[] | false
accepts(type: string[]): string | string[] | false
accepts(...type: string[]): string | string[] | false
acceptsCharsets(): string | string[] | false

Returns the first accepted charset of the specified character sets, based on the request's Accept-Charset HTTP header field. If none of the specified charsets is accepted, returns false.

For more information, or if you have issues or concerns, see accepts.

acceptsCharsets(charset: string): string | string[] | false
acceptsCharsets(charset: string[]): string | string[] | false
acceptsCharsets(...charset: string[]): string | string[] | false
acceptsEncodings(): string | string[] | false

Returns the first accepted encoding of the specified encodings, based on the request's Accept-Encoding HTTP header field. If none of the specified encodings is accepted, returns false.

For more information, or if you have issues or concerns, see accepts.

acceptsEncodings(encoding: string): string | string[] | false
acceptsEncodings(encoding: string[]): string | string[] | false
acceptsEncodings(...encoding: string[]): string | string[] | false
acceptsLanguages(): string | string[] | false

Returns the first accepted language of the specified languages, based on the request's Accept-Language HTTP header field. If none of the specified languages is accepted, returns false.

For more information, or if you have issues or concerns, see accepts.

acceptsLanguages(lang: string): string | string[] | false
acceptsLanguages(lang: string[]): string | string[] | false
acceptsLanguages(...lang: string[]): string | string[] | false
range(size: number, options?: RangeParserOptions): RangeParserRanges | RangeParserResult | undefined

Parse Range header field, capping to the given size.

Unspecified ranges such as "0-" require knowledge of your resource length. In the case of a byte range this is of course the total number of bytes. If the Range header field is not given undefined is returned. If the Range header field is given, return value is a result of range-parser. See more ./types/range-parser/index.d.ts

NOTE: remember that ranges are inclusive, so for example "Range: users=0-3" should respond with 4 users when available, not 3.

is(type: string | string[]): string | boolean | null

Check if the incoming request contains the "Content-Type" header field, and it contains the give mime type.

Examples:

 // With Content-Type: text/html; charset=utf-8
 req.is('html');
 req.is('text/html');
 req.is('text/*');
 // => true

 // When Content-Type is application/json
 req.is('json');
 req.is('application/json');
 req.is('application/*');
 // => true

 req.is('html');
 // => false
upgrade(): WebSocket

Upgrade an HTTP connection to a WebSocket connection by calling Deno.upgradeWebSocket.

Example:

app.get("/ws", async (req, _res, next) => {
  if (req.headers.get("upgrade") === "websocket") {
    const socket = req.upgrade();
    handleSocket(socket);
  } else {
    next();
  }
});
respond(response: { status?: number; statusText?: string; headers?: Headers; body?:
| Uint8Array
| string
; trailers?: () => Promise<Headers> | Headers; }
): void