Responses

Mage's context contains utility methods for configuring responses to requests.

Basic responses

Basic responses include text, JSON, HTML, empty, and not found.

c.text("Hello, world!");

c.json({ message: "Hello, world!" });

c.html("<h1>Hello, world!</h1>");

c.empty();

c.notFound();

Some utilities let you set the status.

c.text("Created", 201);

If you need more control over the response, you can set a response directly.

c.res = new Response("Hello, world!");

Redirecting

By default redirects are temporary 307 but you can change this.

c.redirect("/"); // 307 Temporary Redirect

c.redirect("/new", 308); // 308 Permanent Redirect

Rewriting

You can rewrite the request, internally this is a forwarded fetch.

await c.rewrite("https://somewhere.else");

Or you can rewrite to a local path. (Note that this will make a full fetch request so is sub optimal compared to redirection locally.)

await c.rewrite("/new");

Files

Serve a file from the file system.

await c.file("path/to/file.txt");