Types → Response Types

Response Types

The outgoing side of every route handler. Response is the res object your handler uses to reply. NetworkResponse is what the client receives. MiddlewareHandler is the function type accepted by app:Use().

Response

The res argument passed to every route handler. Call exactly one terminal method (Send, Error) per handler invocation. Chain Status() before the terminal call to set a status code.

export type Response = {
    Send     : (self: Response, data: any?) -> (),
    Error    : (self: Response, message: string) -> (),
    Status   : (self: Response, code: number) -> Response,
    Redirect : (self: Response, route: string) -> (),
}
MethodDescription
res:Send(data?)Send a successful response. data can be any serializable value. Status defaults to 200.
res:Error(message)Send an error response with the given message string. Status defaults to 500 unless overridden with res:Status().
res:Status(code)Set the HTTP-style status code. Returns self for chaining: res:Status(404):Error("not found").
res:Redirect(route)Internally re-dispatch the request to another route. The client sees the response from the redirected handler.
app:Get("player/:id", function(req, res)
    local player = Players:GetPlayerByUserId(req.params.id)
    if not player then
        res:Status(404):Error("player not found")
        return
    end
    res:Send({ name = player.Name })
end)

NetworkResponse

The table the client's Network:Send() promise resolves with. Inspect ok first, then branch on type for more detail.

export type NetworkResponse = {
    type       : "success" | "error" | "timeout",
    status     : number?,
    data       : any?,
    message    : string?,
    compressed : boolean?,
}
FieldTypeDescription
type"success" | "error" | "timeout"Outcome of the request. Check this first.
statusnumber?HTTP-style status code set by res:Status(), or nil for timeouts
dataany?Deserialized response body from res:Send(data)
messagestring?Error message from res:Error(message)
compressedboolean?True if the response was Deflate-compressed
-- Client-side usage
local network = RoExpress.GetNetwork()

network:Send("player/123", { method = "GET" }):andThen(function(res)
    if res.type == "success" then
        print(res.data.name)
    elseif res.type == "error" then
        warn(res.status, res.message)
    else
        warn("request timed out")
    end
end)

MiddlewareHandler

The function type accepted by app:Use(). Middleware runs before every route handler on that app. Return false to block the request with a 403; throw an error to send a 500. Returning nothing (or true) passes the request through.

export type MiddlewareHandler =
    (Player: Player, Payload: Payload) -> (boolean?)
-- Auth guard example
app:Use(function(player, payload)
    if not isAuthorized(player) then
        return false  -- blocked | client receives 403
    end
    -- return nil / true to pass through
end)

Module instance types

Type nameWhat it represents
RoExpress.AppApp instance shape (returned by GetApp())
RoExpress.NetworkNetwork instance shape (returned by GetNetwork())
RoExpress.PortPort instance shape (returned by app:GetPort())
RoExpress.StreamStream module table type
RoExpress.TokenBucketTokenBucket instance type
RoExpress.BroadcastBroadcast module table type

See also

← Types  ·  Request Types | Request, Payload, RouteHandler  ·  Response Object | detailed method reference with examples  ·  Network | NetworkResponse in use  ·  Middleware Guide | MiddlewareHandler patterns