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 }));| Return | Response |
|---|---|
| A string | text/plain; charset=utf-8 |
| An object or array | JSON |
Blob, ArrayBuffer, Uint8Array and the like, ReadableStream, FormData, URLSearchParams | Sent as the body |
A Response | Sent 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()