# Routes

> Status: implemented are markup as HTML, components of the server, their results, `Response`
> going up, `Content<T>`, component values with a result, decorators with guards, wrappers that
> take the body and the request context, routes, and `rooklang/server`; the server of the site, `site/server/main.rk`, is written
> with them, and the guide has a section on them. The decisions are collected at the end of the
> document, together with what is not decided yet. Server markup, components and `rooklang/server`
> remain **experimental** in 1.x and may change between releases.

> This document builds on [components.md](https://github.com/QWERN-9876r/RookLang/blob/HEAD/docs/components.md): components, decorators, context,
> components as values. It describes only what is different on the server.

`route` of `rooklang/server` ties a path to a page. The parameters of the path are the properties
of the page, and the compiler checks them against each other:

```go
import { route, Route, reply } from "rooklang/server"

comp UserPage(id string) { ... }

const ROUTES Route<Html | Response>[] = [
    route("/", Home),
    route("/users/:id", UserPage),
    route("/users/:uid/posts", Posts),   // error: Posts has no property "uid"
]

comp Router(routes Route<Html | Response>[]) {
    for each in routes {
        const Page = each.match(@session.url.pathname)
        if Page != null {
            return <Page />
        }
    }
    return reply(<NotFound />, { status: 404 })
}
```

- **The path is a string literal,** so that its parameters are known: `:id` is a segment that
  gives its text, decoded, to the property `id`.
- **Checked both ways:** each parameter is a property of the page that takes a string, and each
  property the page needs is a parameter. Errors say what to do:
  `UserPage has no property "uid" for ":uid" of "/users/:uid": rename it to ":id", or add the property`.
- **`Route<T>`** has the `path` and `match(pathname)`, which gives the page with the parameters as
  its properties, `comp() T`, or null for another path. `T` is what the page gives; `Route` alone
  is `Route<Html>`. Empty segments do not count: `/users/7/` is `/users/7`.
- **The router itself is Rook code,** a loop over a table, as the goals of this document ask: the
  order of the table is the order of the matches, and a router of a framework adds what it needs,
  the method, a prefix, nested tables.

