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

x/opine/src/response.ts>Response#format

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

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.

Parameters

obj: any

Returns

this

for chaining