Skip to Content
Senri
Routing

Return values

What is sent when you return a string, an object, a Response and so on

import {  } from "@senrijs/senri";

const  = ();

.("/text", () => "Hello");                             // text/plain; charset=utf-8
.("/json", () => ({ : true }));                      // application/json
.("/raw", () => new ("as is", { : 202 }));
ReturnResponse
A stringtext/plain; charset=utf-8
An object or arrayJSON
Blob, ArrayBuffer, Uint8Array and the like, ReadableStream, FormData, URLSearchParamsSent as the body
A ResponseSent as is

On Deno, text with status 200 and no c.header() gets text/plain;charset=UTF-8, the value Deno sets (the meaning is the same; it lets Deno use its faster way of writing the response).

Set the status and headers

Set them with c.status() and c.header(), then return a value.

.("/items", (, ) => {
    .(201);
    .("location", "/items/1");
    return { : 1 };
});

A helper such as c.json(body, status) can set the status in one go (Response helpers).

Returning a function

Returning a function (forgetting to call it) is a type error. At run time, it is also 500.

.("/oops", () => load);   // forgot to call load()
Argument of type '() => () => Promise<{ ok: boolean; }>' is not assignable to parameter of type 'Handler<{}, Record<string, unknown>>'. Type '() => Promise<{ ok: boolean; }>' is not assignable to type 'Promise<HandlerResult> | HandlerResult'.

On this page