Context
The Context
object is passed to route handlers and middleware functions. It contains information about the request and provides methods for generating a response.
Properties
req
The request object. Contains information about the incoming request. This table extends the Lune ServeRequest with the following:
params
:{string: string}
- Path parameters extracted from the route path.
valid
:{string: any}
- Validated data from the request. This table is populated by the validator.
:json()
:- Parse the request body as JSON.
res
The response object. Contains information about the response to be sent. This table is an instance of the Lune ServeResponse.
Avoid modifying the response object directly. Use the Context
methods instead.
Methods
:body
:body(body: buffer | string, status: number?, headers: any?)
Send the response. Attach a body and optional status code and headers.
:text
:text(body: string, status: number?, headers: any?)
Send a text response. Attach a body and optional status code and headers.
:json
:json(body: any, status: number?, headers: any?)
Send a JSON response. Attach a body and optional status code and headers.
:html
:html(body: string, status: number?, headers: any?)
Send an HTML response. Attach a body and optional status code and headers.
:header
:header(name: string, value: string)
Set a response header.
:status
:status(status: number)
Set the response status code.
:redirect
:redirect(url: string, status: number?)
Redirect to a URL. Attach an optional status code (default 302).
:notFound
:notFound()
Send the 404 response defined in the app.
:set
/ :get
:set(key: string, value: any)
:get(key: string): any
Set and get values on the context object. Useful for sharing data between middleware and route handlers.