Router → Named Params

Named Params

Capture route segments by name with :name, or capture and coerce to a typed value with :name=type. Captured values are available on req.params.

Basic syntax

Prefix a segment with : to name it. The value is always a string unless a type annotation is added.

-- :id captures any single segment as a string
app:Get("player/:id", function(req, res)
    print(req.params.id)  -- "123" (string)
end)

-- :id=number coerces to a number
app:Get("player/:id=number", function(req, res)
    print(req.params.id)  -- 123 (number)
end)

Supported types

AnnotationInputResult type
:nameAny segmentstring
:name=numberNumeric stringnumber
:name=intInteger string (no decimal)number
:name=boolean"true" or "false"boolean
:name=vector2Vector2-shaped segmentVector2
:name=vector3Vector3-shaped segmentVector3
:name=cframeCFrame-shaped segmentCFrame
:name=color3Color3-shaped segmentColor3
:name=EnumAny EnumItem (wire format carries the type)EnumItem
:name=Enum.CameraTypeSpecific Enum typeEnumItem
:name=instanceInstance referenceInstance

Examples

-- Integer item ID
app:Get("shop/:itemId=int", function(req, res)
    print(req.params.itemId)  -- always a whole number
end)

-- Boolean flag
app:Get("flag/:on=boolean", function(req, res)
    print(req.params.on)  -- true or false
end)

-- Vector3 position
app:Get("pos/:v=vector3", function(req, res)
    print(req.params.v)  -- Vector3 value
end)

-- Specific Enum type
app:Get("cam/:mode=Enum.CameraType", function(req, res)
    print(req.params.mode)  -- EnumItem
end)

Coercion failure and OnParamError

When a typed param fails coercion (wrong format or out of range), the default behaviour is to respond 400 automatically and skip the handler. Override this with app:OnParamError(fn).

app:OnParamError(function(req, res, paramName, value)
    res:Status(422):Send({ error = "Bad param: " .. paramName })
end)
Pattern constraints. You can restrict which values a named param matches using a Lua pattern in parentheses: :id(%d+)=number. See Constraints for the full syntax.

RouteBuilder — typed URL construction

Router.Route(pattern) pairs a pattern with a type-checked URL builder so your handler registration and URL construction can never drift apart.

RoExpress.Router.Route(pattern: string) → RouteBuilder<T>
local GetCoins = RoExpress.Router.Route("player/:UserId=number/coins") :: RoExpress.RouteBuilder<{
    UserId: number,
}>

App:Get(GetCoins.pattern, function(req, res)
    res:Send({ coins = 100 })
end)

-- .build() is type-checked and always serialises correctly
GetCoins.build({ UserId = 12345 })  -- "/player/12345/coins"

See also

← Router  ·  Constraints | Lua pattern restrictions on param values  ·  Wildcards & Globs | unnamed captures  ·  App | OnParamError handler  ·  Exported Types | RouteBuilder<T>