Routing System
Nex’s routing system follows the principle of “file-as-router,” allowing you to organize your application URLs intuitively through directory structure.
1. Static Routes
The simplest form of routing is direct mapping.
-
src/pages/index.ex->/ -
src/pages/about.ex->/about -
src/pages/contact/index.ex->/contact -
src/pages/blog/list.ex->/blog/list
2. Dynamic Routes [id]
When you need to capture parameters in a URL, use the square bracket syntax for filenames.
-
src/pages/users/[id].ex->/users/123(params:id="123") -
src/pages/posts/[slug].ex->/posts/hello-nex(params:slug="hello-nex")
Accessing parameters in code:
defmodule MyApp.Pages.Users.Id do
use Nex
def mount(%{"id" => id}) do
# id will be "123"
%{user: find_user(id)}
end
end
3. Catch-All Routes [...path]
If you need to capture all remaining path segments, use the [...name] syntax.
-
src/pages/docs/[...path].ex->/docs/intro/getting-started(params:path=["intro", "getting-started"])
4. Parameter Priority
When multiple sources have parameters with the same name, Nex follows this priority (former overrides latter):
-
Path Parameters: e.g.,
idin/users/[id]. -
Query Parameters: e.g.,
qin/search?q=nex.
5. Route Matching Rules
When a URL matches multiple possible route files, Nex sorts them by priority:
- Exact Static Match over Dynamic Parameter Match.
- Fewer Dynamic Parameters first.
- Non-Catch-All over Catch-All.
- Longer Path first.
Example: Visiting /users/new
-
If
src/pages/users/new.exexists, it will take priority oversrc/pages/users/[id].ex.