Skip to content

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:

luau
local common = require("common")
local response = common.response

local Home = {}

-- Handles GET /
function Home.Get()
    return response.json({ message = "Welcome to Mayari!" })
end

return Home

Method Handler Names ​

HTTP methods in Mayari use PascalCase function names:

HTTP MethodHandler Function
GETfunction Handler.Get(req)
POSTfunction Handler.Post(req)
PUTfunction Handler.Put(req)
PATCHfunction Handler.Patch(req)
DELETEfunction Handler.Delete(req)
HEADfunction Handler.Head(req)

Route Types ​

  1. Static Routes

    Fixed URL paths match your folder names 1:1.

    bash
    src/app/
    ├── route.luau                 ➔  GET /
    ├── users/
    │   └── route.luau             ➔  GET /users
    └── settings/
        └── profile/
            └── route.luau         ➔  GET /settings/profile
  2. Dynamic Single Parameters [param]

    Wrap a folder name in brackets [id] to capture a dynamic URL segment. The parameter value is available as a string on req.params.<param>.

    bash
    src/app/
    └── users/
        └── [id]/
            └── route.luau         ➔  GET /users/123

    Example:

    lua
    local 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 User
  3. Catch-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.

    bash
    src/app/
    └── docs/
        └── [...slugs]/
            └── route.luau         ➔  GET /docs/a/b/c
    Requested URLreq.params.slugs Value
    /docs/setup{"setup"}
    /docs/guides/routing{"guides", "routing"}

    Example:

    lua
    local 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.