Routing
Mayari uses a filesystem-based router. Instead of manually mapping URLs to handler functions in a giant config file, your folder hierarchy inside src/app defines your API endpoints.
The route.luau File
To make a directory a public endpoint, place a file named route.luau inside it.
Your route.luau module exports a table containing functions named after the HTTP methods it responds to:
local common = require("common")
local response = common.response
local Home = {}
-- Handles GET /
function Home.Get()
return response.json({ message = "Welcome to Mayari!" })
end
return HomeMethod Handler Names
HTTP methods in Mayari use PascalCase function names:
| HTTP Method | Handler Function |
|---|---|
GET | function Handler.Get(req) |
POST | function Handler.Post(req) |
PUT | function Handler.Put(req) |
PATCH | function Handler.Patch(req) |
DELETE | function Handler.Delete(req) |
HEAD | function Handler.Head(req) |
Route Types
Static Routes
Fixed URL paths match your folder names 1:1.
bashsrc/app/ ├── route.luau ➔ GET / ├── users/ │ └── route.luau ➔ GET /users └── settings/ └── profile/ └── route.luau ➔ GET /settings/profileDynamic Single Parameters
[param]Wrap a folder name in brackets
[id]to capture a dynamic URL segment. The parameter value is available as a string onreq.params.<param>.bashsrc/app/ └── users/ └── [id]/ └── route.luau ➔ GET /users/123Example:
lualocal common = require("common") local response = common.response local User = {} type ReqParam = { params: { id: string } } function User.Get(req: IRequest<ReqParam>) local userId = req.params.id return response.json({ userId = userId, message = "User retrieved" }) end return UserCatch-All / Wildcard Routes
[...param]Prefix the folder parameter with three dots
[...ids]to capture all remaining URL segments. The parameter value is available as a table of strings.bashsrc/app/ └── docs/ └── [...slugs]/ └── route.luau ➔ GET /docs/a/b/cRequested URL req.params.slugsValue/docs/setup{"setup"}/docs/guides/routing{"guides", "routing"}Example:
lualocal common = require("common") local response = common.response local Docs = {} type ReqParams = { params: { slugs: { string } } } function Docs.Get(req: IRequest<ReqParams>) local pathSegments = req.params.slugs local fullPath = table.concat(pathSegments, "/") return response.json({ path = fullPath }) end return Docs
Path Traversal Security Note
If you use catch-all routes to serve or read local disk files, always sanitize the input segments (e.g., prevent .. sequence path traversals) before attempting to read files from your file system.