Basics

Mage aims to have a simple and familiar API.

Handling a request

At its simplest Mage apps are handler functions mapped against routes.

app.get("/hello", async (c) => {
  c.text("Hello, world!");
});

app.get("/goodbye", async (c) => {
  c.text("Goodbye, world!");
});

Middleware

Middleware are the heart of Mage apps. You chain middleware together to handle requests and send back responses.

Chaining

The power of middleware comes when you start chaining them together. Middleware are chained together in the order they are registered. A request will make its way down through the chain and back up again and the final response registered on the context will be sent back to the client.

a() -> b() -> handler() -> b.. -> a..

Registering

Register middleware globally to apply to all routes.

app.use(async (c, next) => {
  await next()
});

Register middleware to specific routes to apply only to those routes.

app.get(
  "/", 
  async (c, next) => {
    await next();
  },
  (c) => {
    c.text("Hello, world!");
  },
);