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

x/opine/src/response.ts>Response

Fast, minimalist web framework for Deno ported from ExpressJS.
Very Popular
Go to Latest
class Response
implements OpineResponse
import { Response } from "https://deno.land/x/opine@2.2.0/src/response.ts";

OpineResponse class.

Properties

headers: Headers
locals: any
status: Status
written: boolean

Methods

addResource(rid: number): void

Add a resource ID to the list of resources to be closed after the .end() method has been called.

append(field: string, value: string | string[]): this

Append additional header field with value val. Value can be either a string or an array of string.

Example:

res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly'); res.append('Warning', '199 Miscellaneous warning'); res.append("cache-control", ["public", "max-age=604800", "immutable"]);

attachment(filename: string): this

Set Content-Disposition header to attachment with optional filename.

Clear a cookie.

clearCookie(name: string, options?: CookieOptions): this
download(
path: string,
filename?: string,
options?: any,
): Promise<this | void>

Transfer the file at the given path as an attachment.

Optionally providing an alternate attachment filename.

Optionally providing an options object to use with res.sendFile().

This function will set the Content-Disposition header, overriding any existing Content-Disposition header in order to set the attachment and filename.

This method uses res.sendFile().

end(body?: DenoResponseBody): Promise<void>

Ends the response process.

etag(chunk: string | Uint8Array | Deno.FileInfo): this

Sets an ETag header.

format(obj: any): this

Respond to the Acceptable formats using an obj of mime-type callbacks.

This method uses req.accepted, an array of acceptable types ordered by their quality values. When "Accept" is not present the first callback is invoked, otherwise the first match is used. When no match is performed the server responds with 406 "Not Acceptable".

Content-Type is set for you, however if you choose you may alter this within the callback using res.type() or res.set('Content-Type', ...).

res.format({ 'text/plain': function(){ res.send('hey'); },

 'text/html': function(){
   res.send('<p>hey</p>');
 },

 'application/json': function(){
   res.send({ message: 'hey' });
 }

});

In addition to canonicalized MIME types you may also use extnames mapped to these types:

res.format({ text: function(){ res.send('hey'); },

 html: function(){
   res.send('<p>hey</p>');
 },

 json: function(){
   res.send({ message: 'hey' });
 }

});

By default Express passes an Error with a .status of 406 to next(err) if a match is not made. If you provide a .default callback it will be invoked instead.

get(field: string): string

Get value for header field.

json(body: ResponseBody): this

Send JSON response.

Examples:

res.json(null);
res.json({ user: 'deno' });

Send JSON response with JSONP callback support.

Examples:

res.jsonp(null);
res.jsonp({ user: 'deno' });
location(url: string): this

Set the location header to url.

The given url can also be "back", which redirects to the Referrer or Referer headers or "/".

Examples:

res.location('/foo/bar').; res.location('http://example.com'); res.location('../login');

redirect(url: string): void

Redirect to the given url with optional response status defaulting to 302.

The resulting url is determined by res.location().

Examples:

res.redirect('/foo/bar'); res.redirect('http://example.com'); res.redirect(301, 'http://example.com'); res.redirect('../login'); // /blog/post/1 -> /blog/login

redirect(statusCode: Status, url: string): void
removeHeader(field: string): this

Remove a header from the response

Examples:

res.removeHeader('Accept');
render(
view: string,
options?: any,
callback?: any,
)

Render view with the given options and optional callback fn. When a callback function is given a response will not be made automatically, otherwise a response of 200 and text/html is given.

Options:

  • cache boolean hinting to the engine it should cache
  • filename filename of the view being rendered
send(body: ResponseBody): this

Send a response.

Examples:

res.send({ some: 'json' });
res.send('<p>some html</p>');
sendFile(path: string, options?: any): Promise<this | void>

Transfer the file at the given path.

Automatically sets the Content-Type response header field.

sendStatus(code: Status): this

Send given HTTP status code.

Sets the response status to code and the body of the response to the standard description from deno's http_status.STATUS_TEXT or the code number if no description.

Examples:

res.sendStatus(200);
set(field: string, value: string): this

Set header field to value, or pass an object of header fields.

Examples:

res.set('Accept', 'application/json');
res.set({
  'Accept-Language': 'en-US, en;q=0.5',
  'Accept': 'text/html',
});
set(obj: Record<string, string>): this
setHeader(field: string, value: string): this

Set header field to value, or pass an object of header fields.

alias for res.set()

Examples:

res.setHeader('Accept', 'application/json');
res.setHeader({
  'Accept-Language': "en-US, en;q=0.5",
  'Accept': 'text/html',
});
setHeader(obj: Record<string, string>): this
setStatus(code: Status): this

Set status code.

This method deviates from Express due to the naming clash with Deno.OpineResponse status property.

type(type: string): this

Set Content-Type response header with type.

Examples:

res.type('.html');
res.type('html');
res.type('json');
res.type('application/json');
res.type('png');
unset(field: string): this

Deletes a header.

vary(field: string | string[]): this

Add field to Vary. If already present in the Vary set, then this call is simply ignored.